第五章
5-5 外星人顏色#3:將練習5-4中的if-else 結構改為if-elif-else 結構。
如果外星人是綠色的,就打印一條消息,指出玩家獲得了5個點。
如果外星人是黃色的,就打印一條消息,指出玩家獲得了10個點。
如果外星人是紅色的,就打印一條消息,指出玩家獲得了15個點。
編寫這個程序的三個版本,它們分別在外星人為綠色、黃色和紅色時打印一條消息。
alien_colors = ['green','yellow','red']
for alien_color in alien_colors:
print("alien_color is %s"%alien_color)
if alien_color == 'green':
print("Get 5 points.")
elif alien_color == 'yellow':
print("Get 10 points.")
else:
print("Get 15 points.")
運行結果
alien_color is green
Get 5 points.
alien_color is yellow
Get 10 points.
alien_color is red
Get 15 points.
5-7 喜歡的水果 :
創建一個列表,其中包含你喜歡的水果,再編寫一系列獨立的if 語句,檢查列表中是否包含特定的水果。
將該列表命名為favorite_fruits ,并在其中包含三種水果。
編寫5條if 語句,每條都檢查某種水果是否包含在列表中,如果包含在列表中,就打印一條消息,如“You really like bananas!”。
favorite_fruit = ['apple', 'banana', 'pineapple', 'kiwifruit']
if 'grape' in favorite_fruit:
print("You really like grape!")
if 'apple' in favorite_fruit:
print("You really like apple!")
if 'banana' in favorite_fruit:
print("You really like banana!")
if 'tomato' in favorite_fruit:
print("You really like tomato!")
if 'kiwifruit' in favorite_fruit:
print("You really like kiwifruit!")
輸出結果:
You really like apple!
You really like banana!
You really like kiwifruit!
5-8 以特殊方式跟管理員打招呼 :
創建一個至少包含5個用戶名的列表,且其中一個用戶名為’admin’ 。想象你要編寫代碼,在每位用戶登錄網站后都打印一條問候消息。遍歷用戶名列表,并向每位用戶打印一條問候消息。
如果用戶名為’admin’ ,就打印一條特殊的問候消息,如“Hello admin, would you like to see a status report?”。
否則,打印一條普通的問候消息,如“Hello Eric, thank you for logging in again”。
username = ['admin', 'Sam', 'Amy', 'Bob']
for item in username:
if item == 'admin':
print('Hello, admin, would you like to see a status report?')
else:
print('Hello %s, thank you for logging in again.' % item)
輸出結果:
Hello, admin, would you like to see a status report?
Hello Sam, thank you for logging in again.
Hello Amy, thank you for logging in again.
Hello Bob, thank you for logging in again.
5-9 處理沒有用戶的情形 :在為完成練習5-8編寫的程序中,添加一條if 語句,檢查用戶名列表是否為空。
如果為空,就打印消息“We need to find some users!”。
刪除列表中的所有用戶名,確定將打印正確的消息。
username = ['admin', 'Sam', 'Amy', 'Bob']
del(username[:])
if len(username) == 0:
print('We need to find some users!')
else:
for item in username:
if item == 'admin':
print('Hello, admin, would you like to see a status report?')
else:
print('Hello %s, thank you for logging in again.' % item)
輸出結果:
We need to find some users!
5-11 序數 :序數表示位置,如1st和2nd。大多數序數都以th結尾,只有1、2和3例外。
在一個列表中存儲數字1~9。
遍歷這個列表。
在循環中使用一個if-elif-else 結構,以打印每個數字對應的序數。輸出內容應為1st 、2nd 、3rd 、4th 、5th 、6th 、7th 、8th 和9th ,但每個序數都獨占一行。
number = [i for i in range(1, 10)]
for i in number:
if i == 1:
print('1st')
elif i == 2:
print('2nd')
elif i == 3:
print('3rd')
else:
print('%dth' % i)
第六章
6-1 人 :使用一個字典來存儲一個熟人的信息,包括名、姓、年齡和居住的城市。該字典應包含鍵first_name 、last_name 、age 和city 。將存儲在該字典中的每項信息都打印出來。
dict = {
'first_name': 'Kaiqi',
'last_name': 'Wang',
'age': 20,
'city': 'Guangzhou',
}
for (k, v) in dict.items():
print("%s : %s" % (k, v))
輸出結果
city : Guangzhou
first_name : Kaiqi
age : 20
last_name : Wang
6-3 詞匯表 :Python字典可用于模擬現實生活中的字典,但為避免混淆,我們將后者稱為詞匯表。
想出你在前面學過的5個編程詞匯,將它們用作詞匯表中的鍵,并將它們的含義作為值存儲在詞匯表中。
以整潔的方式打印每個詞匯及其含義。為此,你可以先打印詞匯,在它后面加上一個冒號,再打印詞匯的含義;也可在一行打印詞匯,再使用換行符(\n )插入一個空行,然后在下一行以縮進的方式打印詞匯的含義。
6-4 詞匯表2 :既然你知道了如何遍歷字典,現在請整理你為完成練習6-3而編寫的代碼,將其中的一系列print 語句替換為一個遍歷字典中的鍵和值的循環。確定該循環正確無誤后,再在詞匯表中添加5個Python術語。當你再次運行這個程序時,這些新術語及其含義將自動包含在輸出中。
dict = {
'apple': '蘋果',
'banana': '香蕉',
'pineaplle': '菠蘿',
'kiwifruit': '獼猴桃'
}
dict['grape'] = '葡萄'
dict['light'] = '光'
dict['computer'] = '計算機'
dict['cup'] = '杯子'
dict['program'] = '程序'
for(k, v) in dict.items():
print("%s: %s" % (k, v))
輸出結果
kiwifruit: 獼猴桃
light: 光
apple: 蘋果
cup: 杯子
computer: 計算機
program: 程序
pineaplle: 菠蘿
grape: 葡萄
banana: 香蕉
6-9 喜歡的地方 :創建一個名為favorite_places 的字典。在這個字典中,將三個人的名字用作鍵;對于其中的每個人,都存儲他喜歡的1~3個地方。為讓這個練
習更有趣些,可讓一些朋友指出他們喜歡的幾個地方。遍歷這個字典,并將其中每個人的名字及其喜歡的地方打印出來。
favorite_place = {
'Amy': 'America',
'Bob': 'Brazil',
'Cathy': 'Canada',
}
for(k, v) in favorite_place.items():
print("%s: %s" % (k, v))
Amy: America
Bob: Brazil
Cathy: Canada
6-10 喜歡的數字 :修改為完成練習6-2而編寫的程序,讓每個人都可以有多個喜歡的數字,然后將每個人的名字及其喜歡的數字打印出來。
favorite_number = {
'Amy': [0, 1],
'Bob': [3, 5, 7, 11],
'Cathy': [65536],
}
for(k, v) in favorite_number.items():
st = ",".join(list(map(str, v)))
print("%s: %s" % (k, st))
輸出結果:
Amy: 0,1
Cathy: 65536
Bob: 3,5,7,11