### 解法不只一種,此處只提供參考解法,並非唯一解答,只要符合題目要求,都算答對。 ### ### Chatper 4 ### 練習 4-2-1 ### # 對題目與變數的認知 apples = 5 oranges = 10 fruits = apples + oranges apples = 20 fruits = apples + oranges ### 練習 4-2-2 ### # 對變數的認知 # python 中的內建函式,若是當成一個變數,你可以對其指派,但是指派後就不能再次當函式使用了 # 修改只需要把第一行程式碼去掉或註解掉即可 #int = 100 # 錯誤。對內建函式進行了變數指派,導致後面 int 方法不能使用 a = '200' b = int(a) print(b) ### 練習 4-3-1 ### (1) >>> str(True) # 結果為 'True' (2) >>> float(3) # 結果為 3.0 (3) >>> str(3.8) # 結果為 '3.8' (4) >>> int(0.5) # 結果為 0 (5) >>> str('4') # 結果為 '4',將 str('4'),型別還是 str ### 練習 4-4-1 ### song = input('Tell me your favorite song: ') print(song) print(song) print(song) ### 練習 4-4-2 ### # 方法一:使用字串的 + 在 print() 中 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) + '!') # 方法二:使用 print 的 , 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 ', older, '!', sep = '') # 方法三:使用 print 的 % name = input("What's your name? ") age = int(input('How old are you? ')) older = age + 25 print('Hi %s! In 25 years you will be %d!' %(name, older)) # 方法四:使用 print 的 format() 方法 name = input("What's your name? ") age = int(input('How old are you? ')) older = age + 25 print('Hi {}! In 25 years you will be {}!'.format(name, older)) ### 練習 4-5-1 ### b = int(input('Enter a number: ')) e = int(input('Enter another number: ')) b_e = b ** e print('b to the power of e is', b_e) ### 練習 4-5-1 ### num1 = int(input('Enter a number: ')) num2 = int(input('Enter another number: ')) print('{} + {} = {}'.format(num1, num2, num1 + num2)) print('{} - {} = {}'.format(num1, num2, num1 - num2)) print('{} * {} = {}'.format(num1, num2, num1 * num2)) print('{} / {} = {}'.format(num1, num2, num1 / num2)) print('{} % {} = {}'.format(num1, num2, num1 % num2)) print('{} // {} = {}'.format(num1, num2, num1 // num2)) ### Chatper 5 ### 練習 5-1 ### pi = 3.14 radius = int(input('請輸入半徑 (Enter the radius): ')) diameter = 2 * radius # 使用公式計算直徑 circle_area = pi * radius * radius # 使用公式計算圓面積 circumference = pi * 2 * radius # 使用公式計算圓周長 print('圓的直徑為: ', diameter) print('圓面積為: ', circle_area) print('圓的周長為: ', circumference) ### Chatper 6 ### 練習 6-1-1 ### s = 'Guten Morgen' s[2:5].upper() ### 練習 6-1-2 ### s = 'RaceTrack' s[1:4].captalize() ### 練習 6-2-1 ### s = 'Eat Work Play Sleep repeat' s = s.replace(' ', 'ing ') s = s[7:22] s = s.lower() print(s) ### 練習 6-2-2 ### s = 'For Tommy and Gina who never backed down.' print(s) name1 = input('please enter first name: ') name2 = input('please enter second name: ') s = s.replace('Tommy and Gina', name1.capitalize() + ' and ' + name2.capitalize()) print(s * 3) ### 練習 6-2-3 ### word = 'TY, pls check for me' word_new = word.replace('TY', 'Thank you') word_new = word_new.replace('pls', 'please') print('網路用語: ' + word + ' 的原始意思是: ' + word_new) ### Chatper 7 ### 練習 7-1-1 ### if 有放假 then 看電影 ### 練習 7-1-2 ### var = 0 if type(var) == int: print("I'm a numbers person.") if type(var) == str: print("I'm a words person.") ### 練習 7-1-3 ### words = input('Tell me anything: ') if ' ' in words: print('This string has spaces.') ### 練習 7-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!') ### 練習 7-1-5 ### num = int(input('Tell me a number: ')) if num >= 0: print('Absolute value:', num) if num < 0: print('Absolute value:', -num) ### 練習 7-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') ### 練習 7-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!') ### Chatper 8 ### 練習 8-1-1 ### # 修正後的程式碼 num = 8 guess = int(input('Guess my number: ')) while guess != num: guess = int(input('Guess again: ')) print('Right!') ### 練習 8-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!') ### 練習 8-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') ### 練習 8-2-2 ### layer = int(input('請問要幾層:')) for i in range(layer): print(' ' * (layer -1 - i) + '*' * (i * 2 + 1)) ### 練習 8-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:]) ### 練習 8-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) ### Chatper 9 ### 練習 9-2-1 ### def calculate_total(price, percent): ''' price, float percent, int calculate the total check including tips Returns total, float ''' price, tip = price * percent / 100 total = price + tip return total ### 練習 9-2-2 ### calculate_total(20, 15) ### 練習 9-2-3 ### my_price = 78.55 my_tip = 20 total = calculate_total(my_price, my_tip) print('Total is: ', total) ### 練習 9-2-4 ### def calculator(x, y): ''' x, float y, float calculate the addition, subtraction, multiplication and division Returns show 'Done!' ''' print('%.1f + %.1f = %.1f' % (x, y, x + y)) print('%.1f - %.1f = %.1f' % (x, y, x - y)) print('%.1f * %.1f = %.1f' % (x, y, x * y)) print('%.1f / %.1f = %.1f' % (x, y, x / y)) return print('Done!') calculator(2, 4) ### 練習 10-1 ### one 是不可變物件 age 是可變物件 ### 練習 10-2-1 ### # 方法一 list(range(1,20,2)) ### 練習 10-2-2 ### # 方法一 str1 = list('machineLearning') ### 練習 11-1-1 ### def get_area(self): """ returns area of a circle """ return 3.14 * self.radius ** 2 # 測試該方法 a = Circle() print(a.get_area()) # 應該為 0 a.change_radius(3) print(a.get_area()) # 應該為 28.26 ### 練習 11-1-2 ### def get_area(self): """ returns area of a rectangle """ return self.length * self.width def get_perimeter(self): """ returns perimeter of a rectangle """ return self.length * 2 + self.width * 2 # 測試該方法 a_rectangle = Rectangle(6, 2) print(a_rectangle.get_area()) # 應該為 12 print(a_rectangle.get_perimeter()) # 應該為 16