5-2 略app
5-3 外星人顏色#1 : 假設在遊戲中剛射殺了一個外星人, 請建立一個名爲alien_color 的變量, 並將其設置爲' green' 、 ' yellow' 或' red' 。
編寫一條if 語句, 檢查外星人是不是綠色的; 若是是, 就打印一條消息, 指出玩家得到了5個點。
編寫這個程序的兩個版本, 在一個版本中上述測試經過了, 而在另外一個版本中未經過(未經過測試時沒有輸出) 。
5-4 外星人顏色#2 : 像練習 5-3那樣設置外星人的顏色, 並編寫一個if-else 結構。
若是外星人是綠色的, 就打印一條消息, 指出玩家因射殺該外星人得到了5個點。
若是外星人不是綠色的, 就打印一條消息, 指出玩家得到了10個點。
編寫這個程序的兩個版本, 在一個版本中執行if 代碼塊, 而在另外一個版本中執行else 代碼塊。
5-5 外星人顏色#3 : 將練習 5-4中的if-else 結構改成if-elif-else 結構。
若是外星人是綠色的, 就打印一條消息, 指出玩家得到了5個點。
若是外星人是黃色的, 就打印一條消息, 指出玩家得到了10個點。
若是外星人是紅色的, 就打印一條消息, 指出玩家得到了15個點。
編寫這個程序的三個版本, 它們分別在外星人爲綠色、 黃色和紅色時打印一條消息。
5-6人生的不一樣階段 : 設置變量age 的值, 再編寫一個if-elif-else 結構, 根據age 的值判斷處於人生的哪一個階段。
若是一我的的年齡小於2歲, 就打印一條消息, 指出他是嬰兒。
若是一我的的年齡爲2(含) ~4歲, 就打印一條消息, 指出他正蹣跚學步。
若是一我的的年齡爲4(含) ~13歲, 就打印一條消息, 指出他是兒童測試
若是一我的的年齡爲13(含) ~20歲, 就打印一條消息, 指出他是青少年。
若是一我的的年齡爲20(含) ~65歲, 就打印一條消息, 指出他是成年人。
若是一我的的年齡超過65(含) 歲, 就打印一條消息, 指出他是老年人。
5-7 喜歡的水果 : 建立一個列表, 其中包含你喜歡的水果, 再編寫一系列獨立的if 語句, 檢查列表中是否包含特定的水果。
將該列表命名爲favorite_fruits , 並在其中包含三種水果。
編寫5條if 語句, 每條都檢查某種水果是否包含在列表中, 若是包含在列表中, 就打印一條消息, 如「You really like bananas!」。網站
答:5-3ui
#1
alien_color = [ 'green']
if 'green' in alien_color :
print("Player have 5 points!")3d
#2
alien_color = ['red']
if 'green' in alien_color :
print("Player have 5 points!")blog
輸出結果:three
答:5-4遊戲
#1
alien_color = ['green']
if 'green' in alien_color :
print("Player have 5 points!")
else:
print("Player have 10 points!")
#2
alien_color = [ 'red' ]
if 'green' in alien_color :
print("Player have 5 points!")
else:
print("Player have 10 points!")it
輸出結果:登錄
答 :5-5
#1
alien_color = ['green','yellow','red'] #其餘兩個版本就是將green改一下
if 'green' in alien_color :
print("Player have 5 points!")
elif 'yellow' in alien_color :
print("Player have 10 points!")
elif 'red' in alien_color :
print("Player have 15 points!")
輸出結果:
答:5-6
age = 20
if age < 2 :
print("He is a baby")
if age < 4:
print("He is toddler")
if age < 13:
print("He is He is a child")
if age < 20:
print("He is a teenager")
if age < 65:
print("He is an adult")
if age > 65:
print("He is the elderly")
輸出結果:
答:5-7
favorite_fruits =['tomato','banana','apple','pear','peach']
if 'tomato' in favorite_fruits:
print("you really like tomato")
if 'banana' in favorite_fruits:
print("you really like banana")
if 'apple' in favorite_fruits:
print("you really like apple")
if 'pear' in favorite_fruits:
print("you really like pear")
if 'peach' in favorite_fruits:
print("you really like peach")
輸出結果:
5-8 以特殊方式跟管理員打招呼 : 建立一個至少包含5個用戶名的列表, 且其中一個用戶名爲' admin' 。 想象你要編寫代碼, 在每位用戶登陸網站後都打印一條問
候消息。 遍歷用戶名列表, 並向每位用戶打印一條問候消息。
若是用戶名爲' admin' , 就打印一條特殊的問候消息, 如「Hello admin, would you like to see a status report?」。
不然, 打印一條普通的問候消息, 如「Hello Eric, thank you for logging in again」。
5-9 處理沒有用戶的情形 : 在爲完成練習 5-8編寫的程序中, 添加一條if 語句, 檢查用戶名列表是否爲空。
若是爲空, 就打印消息「We need to find some users!」。
刪除列表中的全部用戶名, 肯定將打印正確的消息。
5-10 檢查用戶名 : 按下面的說明編寫一個程序, 模擬網站確保每位用戶的用戶名都獨一無二的方式。
建立一個至少包含5個用戶名的列表, 並將其命名爲current_users 。
再建立一個包含5個用戶名的列表, 將其命名爲new_users , 並確保其中有一兩個用戶名也包含在列表current_users 中。
遍歷列表new_users , 對於其中的每一個用戶名, 都檢查它是否已被使用。 若是是這樣, 就打印一條消息, 指出須要輸入別的用戶名; 不然, 打印一條消息, 指
出這個用戶名未被使用。
確保比較時不區分大消息; 換句話說, 若是用戶名' John' 已被使用, 應拒絕用戶名' JOHN' 。
5-11 序數 : 序數表示位置, 如1st和2nd。 大多數序數都以th結尾, 只有一、 2和3例外。
在一個列表中存儲數字1~9。
遍歷這個列表。
在循環中使用一個if-elif-else 結構, 以打印每一個數字對應的序數。 輸出內容應爲1st 、 2 nd 、 3rd 、 4 th 、 5th 、 6th 、 7 th 、 8th 和9th , 但每一個序
數都獨佔一行。
答:5-8
users = ['admin','mumu','lin','mu']
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
users = [ ]
if users:
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.')
else:
print("We need to find some users!")
輸出結果:
答:5-10
current_users = ['one','two','three','four','five']
new_users = ['one','ttwo','tthree','ffour','five','six']
for new_user in new_users:
if new_user in current_users:
print("'"+ new_user + "'"+ ", " + "It is already occupied, please enter another username")
else:
print("Username is not occupied")
輸出結果:
答:5-11
如今作不出來,之後在補上