### ex6-1 ### print('abc') print('abc') print('abc') ### ex6-2 (6-1.py) ### s = input('請輸入顯示內容: ') n = int(input('請輸入顯示次數: ')) while n > 0: print(s) n = n - 1 ### ex6-3 (6-2.py) ### lucky_num = 77 guess = int(input('Guess a Number(0-99): ')) tries = 1 while guess != lucky_num: print('You tried to guess', tries, 'times') guess = int(input('Guess again: ')) tries += 1 print('You got it!') ### ex6-4 (6-3.py) ### secret = 'code' max_tries = 100 guess = input('Guess a word: ') tries = 1 while guess != secret: print('You tried to guess', tries, 'times') if tries == max_tries: print('You ran out of tries.') break guess = input('Guess again: ') tries += 1 if tries <= max_tries and guess == secret: print('You got it!') ### ex6-5 (6-4.py) ### i = 1 while(i < 10): if i == 5: i += 1 continue print(i, end = ' ') i += 1 print('End') ### ex6-6 (6-5.py) ### s = '' while s != '喵喵': if s != '': print('不對喔!') s = input('請輸入通關密語: ') if s == 'out': break else: print('恭喜你過關了') print('再見!') ### ex6-7 (6-6.py) ### while True: print('when will it end?!') ### ex6-8 ### # 使用 for 迴圈 (6-7a.py) for i in range(10): print('Hello Python') # 不使用迴圈 (6-7b.py) print('Hello Python') print('Hello Python') print('Hello Python') print('Hello Python') print('Hello Python') print('Hello Python') print('Hello Python') print('Hello Python') print('Hello Python') print('Hello Python') ### ex6-9 (6-8.py) ### for v in range(2): print('var v is', v) ### ex6-10 (6-9.py) ### for i in range(10, 1, -1): print(i, end = ' ') print() for i in range(0, 9, 2): print(i, end = ' ') ### ex6-11 (6-10.py) ### for i in range(1, 10): for j in range(1, 10): print(j, 'x', i, '=', j * i, end = ' ') print() ### ex6-12 (6-11.py) ### for i in range(1, 20): if i == 5: continue print(i, end = ' ') if i == 10: break print('END') ### ex6-13 (6-12.py) ### num = int(input('Please specify the range(0~N): ')) for i in range(2, num + 1): for j in range(2, i): if(i % j == 0): break else: print(i) ### ex6-14 ### # 字串容器 >>> string = '52python' # 字串容器:由字元組成 >>> string[0] # 字串以索引取值,結果為:'5' # tuple 容器 >>> tuple = (1, '2', 3) # tuple 容器:由資料物件組成 >>> tuple[0] # tuple 以索引取值,結果為:1 # 串列容器 (list) >>> list = [1, '2', 3] # 串列容器:由資料物件組成 >>> list[2] # 串列以索引取值,結果為:'2' # 集合容器 (set) >>> set = {1, '2', 3} # 集合容器:由資料物件組成,無序所以沒有索引取值 # 字典容器 (directory) >>> dick = {'A':1, 'B':'2', 'C':3} # 字典容器:由資料物件組成,以鍵:值表示 >>> dick['B'] # 字典以鍵取值,結果為:'2' # Python 的第三方資料結構:numpy.array >>> import numpy as np # 匯入 numpy >>> a = np.array([10, 2, 45, 32, 24]) # 建立五個元素 >>> len(a) # 計算元素個數 >>> a[2:4] # 取出第 2、3 個元素 >>> a[:4] # 取出第 1 ~ 3 個元素 ### ex6-15 (6-13.py) ### for ch in 'Python is fun!': print('the character is', ch) ### ex6-16 (6-14.py) ### my_string = 'Python is fun!' len_s = len(my_string) for i in range(len_s): print('the character is', my_string[i]) ### ex6-17 (6-15.py) ### winners = ('Peter', 'Mark', 'Joy') print('恭喜以下得獎人:') for name in winners: print(name) ### ex6-18 (6-16.py) ### prizes = ('頭獎', '二獎', '三獎') winners = ('Peter', 'Mark', 'Joy') print('恭喜以下得獎人:') for i in range(3): print(prizes[i]+ ':' + winners[i]) ### ex6-19 (6-17.py) ### winners = (('頭獎', 'Peter'), ('二獎', 'Mark'), ('三獎', 'Joy')) print('恭喜以下得獎人:') for e in winners: print(e[0] + ':' + e[1]) ### ex6-20 ### # for 迴圈 (6-18a.py) for x in range(3): print('var x is', x) # while 迴圈 (6-18b.py) x = 0 while x < 3: print('var x is', x) x += 1