### 每一行自己寫,每行都要了解,要了解程式的執行順序。 ### ### 寫不出來就偷看或找解答,但是要完全的 tracing code。 ### ### 解法不只一種,此處只提供參考解法,並非唯一解答,只要符合題目要求,都算答對。 ### ### Chatper 1 # 練習 1-2 (hw1-1-HelloWorld.py) print('Hello, Python!') print('整數 3 + 2 相加,結果為: ') print(3 + 2) ### Chatper 2 # 練習 2-2 from time import sleep sleep(1) print('*') sleep(1) print('**') sleep(1) print('***') sleep(1) print('****') sleep(1) print('*****') ### Chatper 3 # 練習 3-4 name = input("What's your name? ") age = int(input('How old are you? ')) older = age + 25 print('Hi ' + name + '! In 25 years you will be ' + str(older) + '!') # 練習 3-5 b = int(input('Enter a number: ')) e = int(input('Enter a number: ')) b_e = b ** e print('b to the power of e is', b_e) ### Chatper 4 # 練習 4-2-1 fah = 75 cel = (fah - 32) / 1.8 print(cel) # 練習 4-2-2 miles = 5 km = miles / 0.62137 meters = 1000 * km print("miles") print(miles) print("km") print(km) print("meters") ### Chapter 5 # 觀念驗證 5.1 1. 否 2. 是 3. 否 4. 否 5. 否 # 觀念驗證 5.2 1. num is less than 10 Finished 2. Finished 3. Finished # 觀念驗證 5.3 1. if " " in word: 2. if num1 + num2 < 0: # 觀念驗證 5.4 略 # 觀念驗證 5.5 1. True 2. True 3. False 4. True 5. 226007 # 練習 5-1-2 var = 0 if type(var) == int: print("I'm a numbers person.") if type(var) == str: print("I'm a words person.") # 練習 5-1-3 words = input('Tell me anything: ') if ' ' in words: print('This string has spaces.') # 練習 5-1-4 print('Guess my number! ') secret = 7 num = int(input("What's your guess? (0-99) ")) if num < secret: print('Too low.') if num > secret: print('Too high.') if num == secret: print('You got it!') # 練習 5-1-5 num = int(input('Tell me a number: ')) if num >= 0: print('Absolute value: ', num) if num < 0: print('Absolute value: ', -num) # 觀念驗證 5.6 數字有 3 種狀況,使用 if-else 語法的話,只能判斷正數或負數,當使用者輸入 0 時,無法顯示正確的訊息。 # 觀念驗證 5.7 num 為 -3,輸出:num is negative num 為 0,輸出:num is zero num 為 2,輸出:num is positive num 為 1,輸出:num is positive # 觀念驗證 5.8 略 # 觀念驗證 5.9 num if-elif-else if ------------------------------------------------------------------------------- 20 num is greater than 3 num is greater than 3 Finished. Finished. ------------------------------------------------------------------------------- 9 num is less than 10 num is less than 10 Finished. num is greater than 3 Finished. ------------------------------------------------------------------------------- 5 num is less than 6 num is less than 6 Finished. num is less than 10 num is greater than 3 Finished. ------------------------------------------------------------------------------- 0 num is less than 6 num is less than 6 Finished. num is less than 10 Finished. # 練習 5-2-1 num1 = int(input('One number: ')) num2 = int(input('Another number: ')) if num1 < num2: print('first number is less than the second number') elif num2 < num1: print('first number is greater than the second number') else: print('numbers are equal') # 練習 5-2-2 words = input('Enter anything: ') if 'a' in words and 'e' in words and 'i' in words and 'o' in words and 'u' in words: print('You have all the vowels!') if words[0] == 'a' and words[-1] == 'z': print("And it's sort of alphabetical!") ### Chapter 6 # 觀念驗證 6.1 s = input('請輸入顯示內容: ') n = int(input('請輸入顯示次數: ')) while(n > 0): print(n, s) n = n - 1 # 觀念驗證 6.2 因為 tries 先加 1 才印出次數,因此顯示的猜測次數會比實際猜的次數多 1 次。 # 練習 6-1-1 # 此為猜數字,修正後的程式碼如下 num = 8 guess = int(input('Guess my number: ')) while guess != num: guess = int(input('Guess again: ')) print('Right!') # 練習 6-1-2 play = input('Play? y or yes: ') while play == 'y' or play == 'yes': num = 8 guess = int(input('Guess a number! ')) while guess != num: guess = int(input('Guess again: ')) print('Right!') play = input('Play? y or yes: ') print('See you later!') # 觀念驗證 6.3 1. 0 2. 0, 1, 2, 3, 4 3. 0, 1, 2, 3, 4, 5, 6, ..., 99 # 觀念驗證 6.4 1. 0, 1, 2, 3, 4, 5, 6, 7, 8 2. 3, 4, 5, 6, 7 3. -2, 0, 2 4. 5, 2, -1, -4 5. (無資料) # 觀念驗證 6.5 Mins = int(input('please input Minutes to countdown: ')) for i in range(Mins-1, -1, -1): for j in range(59, -1, -1): print(i, ':', j) # 練習 6-2-1 counter = 0 for num in range(1, 100): if num % 3 == 0: counter += 1 print(counter, 'numbers are even and divisible by 3') # 練習 6-2-2 layer = int(input('請問要幾層: ')) for i in range(layer): print(' ' * (layer -1 - i) + '*' * (i * 2 + 1)) # 觀念驗證 6.6 vowels = 'aeiou' words = input('Tell me something: ') for letter in words: if letter in vowels: print('vowel') # 觀念驗證 6.7 Name = (('張', '天才'), ('王', '子帥'), ('陳', '美美')) print('全名是: ') for e in Name: print(e[0] + e[1]) # 觀念驗證 6.8 password = 'robot fort flower graph' space_count = 0 for ch in password: if ch == ' ': space_count += 1 print(space_count) # 我們也可以使用 password.count(' '),來計算字串內有幾個空白。 # 練習 6-3-1 names = input('Tell me some names, separated by spaces: ') name = '' for ch in names: if ch == ' ': print('Hi', name) name = '' else: name += ch # 需額外處理最後一個名字,因為最後一個名字之後沒有空白 lastspace = names.rfind(' ') print('Hi', names[lastspace + 1:]) # 練習 6-3-2 wish_list = () item = input('What do you want to buy? ') while (item != '沒了'): wish_list = wish_list + (item, ) item = input('What do you want to buy? ') print('你購買的物品如下: ') for e in wish_list: print('● ' + e)