Python練手例子(6)

3一、請輸入星期幾的第一個字母來判斷一下是星期幾,若是第一個字母同樣,則繼續判斷第二個字母。python

程序分析:用狀況語句比較好,若是第一個字母同樣,則判斷用狀況語句或if語句判斷第二個字母。函數

letter = input('Please input:') if letter == 'S': print('Please input second letter:') letter = input('Please input:') if letter == 'a': print('Saturday') elif letter == 'u': print('Sunday') else: print('Date error') elif letter == 'F': print('Friday') elif letter == 'M': print('Monday') elif letter == 'T': print('Please input second letter') letter = input('Please input:') if letter == 'u': print('Tuesday') elif letter == 'h': print('Thursday') else: print('Date error') elif letter == 'W': print('Wednesday') else: print('Date error')

 

3二、按相反的順序輸出列表的值。字體

#python 3.7
 a = ['one', 'two', 'three'] for i in a[::-1]: print(i)

 

3三、按逗號分隔列表。spa

#python 3.7
 L = [1, 2, 3, 4, 5] s = ','.join(str(n) for n in L) print(s)

 

3四、練習函數調用。.net

#python 3.7

def hello_world(): print('hello world') def three_hellos(): for i in range(3): hello_world() if __name__ == '__main__': three_hellos()

 

3五、文本顏色設置。code

class bcolors: HEADER = '\033[95m' OKBLUE = '\033[94m' OKGREEN = '\033[92m' WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m'
print(bcolors.WARNING + "警告的顏色字體?" + bcolors.ENDC)

 

3六、求100以內的素數。blog

#python 3.7
 lower = int(input('輸入區間最小值:')) upper = int(input('輸入區間最大值:')) for num in range(lower, upper + 1): if num > 1: for i in range(2, num): if (num % i) == 0: break
        else: print(num)

 

 

 

參考資料:three

一、Python 100例input

二、文本顏色設置:https://blog.csdn.net/jacson_bai/article/details/71032462class

相關文章
相關標籤/搜索