5-1 條件測試 :編寫一系列條件測試;將每個測試以及你對其結果的預測和實際結果都打印出來。你編寫的代碼應類似於下面這樣:
-
car = 'subaru'
print("Is car == 'subaru'? I predict True.")
print(car == 'subaru')print("\nIs car == 'audi'? I predict False.")
print(car == 'audi') -
詳細研究實際結果,直到你明白了它為何為True 或False 。
-
創建至少10個測試,且其中結果分別為True 和False 的測試都至少有5個。
5-2 更多的條件測試 :你並非只能創建10個測試。如果你想嘗試做更多的比較,可再編寫一些測試,並將它們加入到conditional_tests.py中。對於下面列出的各種測試,至少編寫一個結果為True 和False 的測試。
- 檢查兩個字符串相等和不等。
- 使用函數lower() 的測試。
- 檢查兩個數字相等、不等、大於、小於、大於等於和小於等於。
- 使用關鍵字and 和or 的測試。
- 測試特定的值是否包含在列表中。
- 測試特定的值是否未包含在列表中。
5-3 外星人顏色#1 :假設在游戲中剛射殺了一個外星人,請創建一個名為alien_color 的變量,並將其設置為'green' 、'yellow' 或'red' 。
- 編寫一條if 語句,檢查外星人是否是綠色的;如果是,就打印一條消息,指出玩家獲得了5個點。
- 編寫這個程序的兩個版本,在一個版本中上述測試通過了,而在另一個版本中未通過(未通過測試時沒有輸出)。
-
alien_color = "green"
if alien_color == 'green':
score = '5'
print("你剛剛獲得了" + score + "分!")
if alien_color != 'green':
print("")
-
alien_color = "red"
if alien_color == 'green':
score = '5'
print("你剛剛獲得了" + score + "分!")
if alien_color != 'green':
print("")
5-4 外星人顏色#2 :像練習5-3那樣設置外星人的顏色,並編寫一個if-else 結構。
- 如果外星人是綠色的,就打印一條消息,指出玩家因射殺該外星人獲得了5個點。
- 如果外星人不是綠色的,就打印一條消息,指出玩家獲得了10個點。
- 編寫這個程序的兩個版本,在一個版本中執行if 代碼塊,而在另一個版本中執行else 代碼塊。
-
alien_color = "green"
if alien_color == 'green':
score = '5'
else:
score = '10'
print("你剛剛獲得了" + score + "分!")
-
alien_color = "red"
if alien_color == 'green':
score = '5'
else:
score = '10'
print("你剛剛獲得了" + score + "分!")
5-5 外星人顏色#3 :將練習5-4中的if-else 結構改為if-elif-else 結構。
- 如果外星人是綠色的,就打印一條消息,指出玩家獲得了5個點。
- 如果外星人是黃色的,就打印一條消息,指出玩家獲得了10個點。
- 如果外星人是紅色的,就打印一條消息,指出玩家獲得了15個點。
- 編寫這個程序的三個版本,它們分別在外星人為綠色、黃色和紅色時打印一條消息。
-
alien_color = "green"
if alien_color == 'green':
score = '5'
elif alien_color == 'yellow':
score = '10'
elif alien_color == 'red':
score = '15'
print("你剛剛獲得了" + score + "分!")
-
alien_color = "yellow"
if alien_color == 'green':
score = '5'
elif alien_color == 'yellow':
score = '10'
elif alien_color == 'red':
score = '15'
print("你剛剛獲得了" + score + "分!")
-
alien_color = "red"
if alien_color == 'green':
score = '5'
elif alien_color == 'yellow':
score = '10'
elif alien_color == 'red':
score = '15'
print("你剛剛獲得了" + score + "分!")
5-6 人生的不同階段 :設置變量age 的值,再編寫一個if-elif-else 結構,根據age 的值判斷處於人生的哪個階段。
- 如果一個人的年齡小於2歲,就打印一條消息,指出他是嬰兒。
- 如果一個人的年齡為2(含)~4歲,就打印一條消息,指出他正蹣跚學步。
- 如果一個人的年齡為4(含)~13歲,就打印一條消息,指出他是兒童。
- 如果一個人的年齡為13(含)~20歲,就打印一條消息,指出他是青少年。
- 如果一個人的年齡為20(含)~65歲,就打印一條消息,指出他是成年人。
- 如果一個人的年齡超過65(含)歲,就打印一條消息,指出他是老年人。
age = 6
if age < 2:
message = "嬰兒"
elif age < 4:
message = "蹣跚學步"
elif age < 13:
message = "兒童"
elif age < 20:
message = "青年"
elif age < 65:
message = "成年"
else:
message = '老年'
print("他現在是" + message + "了。")
5-7 喜歡的水果 :創建一個列表,其中包含你喜歡的水果,再編寫一系列獨立的if 語句,檢查列表中是否包含特定的水果。
- 將該列表命名為favorite_fruits ,並在其中包含三種水果。
- 編寫5條if 語句,每條都檢查某種水果是否包含在列表中,如果包含在列表中,就打印一條消息,如“You really like bananas!”。
favorite_fruits = ["apple",'banana','strawberry']
if 'watermelon' in favorite_fruits:
print("You really like watermelon")
elif 'melon' in favorite_fruits:
print("You really like melon")
elif 'lemon' in favorite_fruits:
print("You really like lemon")
elif 'peach' in favorite_fruits:
print("You really like peach")
elif 'apple' in favorite_fruits:
print("You really like apple")
else:
print("沒有我喜歡的水果。")
5-8 以特殊方式跟管理員打招呼 :創建一個至少包含5個用戶名的列表,且其中一個用戶名為'admin' 。想象你要編寫代碼,在每位用戶登錄網站后都打印一條問候消息。遍歷用戶名列表,並向每位用戶打印一條問候消息。
- 如果用戶名為'admin' ,就打印一條特殊的問候消息,如“Hello admin, would you like to see a status report?”。
- 否則,打印一條普通的問候消息,如“Hello Eric, thank you for logging in again”。
users = ['a','b','admin','c','d']
for user in users:
if user == "admin":
print("Hello " + user + ", would you like to see a status report?")
else:
print("Hello " + user + ", thank you for logging in again")
5-9 處理沒有用戶的情形 :在為完成練習5-8編寫的程序中,添加一條if 語句,檢查用戶名列表是否為空。
- 如果為空,就打印消息“We need to find some users!”。
- 刪除列表中的所有用戶名,確定將打印正確的消息。
users = ['a','b','admin','c','d']
for user in users:
if user == "admin":
print("Hello " + user + ", would you like to see a status report?")
else:
print("Hello " + user + ", thank you for logging in again")
if len(users) == 0:
print("We need to find some users!")
else:
users.clear()
print("We need to find some users!")
5-10 檢查用戶名 :按下面的說明編寫一個程序,模擬網站確保每位用戶的用戶名都獨一無二的方式。
- 創建一個至少包含5個用戶名的列表,並將其命名為current_users 。
- 再創建一個包含5個用戶名的列表,將其命名為new_users ,並確保其中有一兩個用戶名也包含在列表current_users 中。
- 遍歷列表new_users ,對於其中的每個用戶名,都檢查它是否已被使用。如果是這樣,就打印一條消息,指出需要輸入別的用戶名;否則,打印一條消息,指出這個用戶名未被使用。
- 確保比較時不區分大消息;換句話說,如果用戶名'John' 已被使用,應拒絕用戶名'JOHN' 。
current_users = ['a','b','c','d','e']
new_users = ['D','E','F','G','H']
for new in new_users:
if new.lower() in [current_user.lower() for current_user in current_users]:
print(new + "被占用")
else:
print(new + "可以使用")
5-11 序數 :序數表示位置,如1st和2nd。大多數序數都以th結尾,只有1、2和3例外。
- 在一個列表中存儲數字1~9。
- 遍歷這個列表。
- 在循環中使用一個if-elif-else 結構,以打印每個數字對應的序數。輸出內容應為1st 、2nd 、3rd 、4th 、5th 、6th 、7th 、8th 和9th ,但每個序數都獨占一行。
numbers = list(range(1,10))
for i in numbers:
if i == 1:
print(str(i) + "st")
elif i == 2:
print(str(i) + "nt")
else:
print(str(i) + "th")