第五章python作業

5-3 外星人顏色#1 :假設在遊戲中剛射殺了一個外星人,請創建一個名爲alien_color 的變量,並將其設置爲'green' 、'yellow' 或'red' 。編寫一條if 語句,檢查外星人是否是綠色的;如果是,就打印一條消息,指出玩家獲得了5個點。編寫這個程序的兩個版本,在一個版本中上述測試通過了,而在另一個版本中未通過(未通過測試時沒有輸出)。

alien_color='green'
if alien_color == 'green':
	print("You get 5 points")
"""
output:
You get 5 points
"""
alien_color='yellow'
if alien_color == 'green':
	print("You get 5 points")
"""
output:
"""
5-4 外星人顏色#2 :像練習5-3那樣設置外星人的顏色,並編寫一個if-else 結構。
如果外星人是綠色的,就打印一條消息,指出玩家因射殺該外星人獲得了5個點。
如果外星人不是綠色的,就打印一條消息,指出玩家獲得了10個點。

編寫這個程序的兩個版本,在一個版本中執行if 代碼塊,而在另一個版本中執行else 代碼塊。

alien_color='green'
if alien_color == 'green':
	print("You kill a alien.You get 5 points")
else:
	print("You get 10 points")
"""
output:
You kill a alien.You get 5 points
"""
alien_color='red'
if alien_color == 'green':
	print("You kill a alien.You get 5 points")
else:
	print("You get 10 points")
"""
output:
You get 10 points
"""
5-5 外星人顏色#3 :將練習5-4中的if-else 結構改爲if-elif-else 結構。
如果外星人是綠色的,就打印一條消息,指出玩家獲得了5個點。
如果外星人是黃色的,就打印一條消息,指出玩家獲得了10個點。
如果外星人是紅色的,就打印一條消息,指出玩家獲得了15個點。

編寫這個程序的三個版本,它們分別在外星人爲綠色、黃色和紅色時打印一條消息。

alien_color='red'
if alien_color == 'green':
	print("You kill a green alien.You get 5 points")
elif alien_color == 'yellow':
	print("You kill a yellow alien.You get 10 points")
else:
	print("You kill a red alien.You get 15 points")
"""
output:
You kill a red alien.You get 15 points
"""
5-6 人生的不同階段 :設置變量age 的值,再編寫一個if-elif-else 結構,根據age 的值判斷處於人生的哪個階段。
如果一個人的年齡小於2歲,就打印一條消息,指出他是嬰兒。
如果一個人的年齡爲2(含)~4歲,就打印一條消息,指出他正蹣跚學步。
如果一個人的年齡爲4(含)~13歲,就打印一條消息,指出他是兒童。
如果一個人的年齡爲13(含)~20歲,就打印一條消息,指出他是青少年。
如果一個人的年齡爲20(含)~65歲,就打印一條消息,指出他是成年人。

如果一個人的年齡超過65(含)歲,就打印一條消息,指出他是老年人。

age=19
if age < 2:
	print("He is a baby")
elif age < 4:
	print("He is a toddler")
elif age < 13:
	print("He is a child")
elif age < 20:
	print("He is a teenager")
elif age < 65:
	print("He is an adult")
else:
	print("He is an old man")
"""
output:
He is a teenager
"""
5-8 以特殊方式跟管理員打招呼 :創建一個至少包含5個用戶名的列表,且其中一個用戶名爲'admin' 。想象你要編寫代碼,在每位用戶登錄網站後都打印一條問
候消息。遍歷用戶名列表,並向每位用戶打印一條問候消息。
如果用戶名爲'admin' ,就打印一條特殊的問候消息,如“Hello admin, would you like to see a status report?”。

否則,打印一條普通的問候消息,如“Hello Eric, thank you for logging in again”。

name_list=["Jenny","Eric","admin","Kobe","Alice"]
for name in name_list:
	if name == 'admin':
		print("Hello admin,would you like to see a status report?")
	else:
		print("Hello "+name+", thank you for logging in again")
"""
output:
Hello Jenny, thank you for logging in again
Hello Eric, thank you for logging in again
Hello admin,would you like to see a status report?
Hello Kobe, thank you for logging in again
Hello Alice, thank you for logging in again
"""
5-9 處理沒有用戶的情形 :在爲完成練習5-8編寫的程序中,添加一條if 語句,檢查用戶名列表是否爲空。
如果爲空,就打印消息“We need to find some users!”。
刪除列表中的所有用戶名,確定將打印正確的消息。
name_list=["Jenny","Eric","admin","Kobe","Alice"]
if name_list:
	name_list.clear()
	print("Delete all users' name")
else:
	print("We need to find some users!")
"""
output:
Delete all users' name
"""
name_list=[]
if name_list:
	name_list.clear()
	print("Delete all users' name")
else:
	print("We need to find some users!")
"""
output:
We need to find some users!
"""

5-10 檢查用戶名 :按下面的說明編寫一個程序,模擬網站確保每位用戶的用戶名都獨一無二的方式。
創建一個至少包含5個用戶名的列表,並將其命名爲current_users 。
再創建一個包含5個用戶名的列表,將其命名爲new_users ,並確保其中有一兩個用戶名也包含在列表current_users 中。
遍歷列表new_users ,對於其中的每個用戶名,都檢查它是否已被使用。如果是這樣,就打印一條消息,指出需要輸入別的用戶名;否則,打印一條消息,指
出這個用戶名未被使用。

確保比較時不區分大消息;換句話說,如果用戶名'John' 已被使用,應拒絕用戶名'JOHN' 。

current_users=['John','Bob','Alice','Jane','James']
new_users=['james','JOHN','tony','jimmy','cindy']
for name in new_users:
	check=False
	for name1 in current_users:
		if name.upper() == name1.upper():
			print('This name has been used, You need to input a new name')
			check=True
			break
	if check == False:
		print("This name is new")
"""
output:
This name has been used, You need to input a new name
This name has been used, You need to input a new name
This name is new
This name is new
This name is new
"""
5-11 序數 :序數表示位置,如1st和2nd。大多數序數都以th結尾,只有1、2和3例外。
在一個列表中存儲數字1~9。
遍歷這個列表。

在循環中使用一個if-elif-else 結構,以打印每個數字對應的序數。輸出內容應爲1st 、2nd 、3rd 、4th 、5th 、6th 、7th 、8th 和9th ,但每個序數都獨佔一行。

num_list=list(range(1,10));
for num in num_list:
	if num == 1:
		print(str(num)+"st")
	elif num == 2:
		print(str(num)+'nd')
	elif num ==3:
		print(str(num)+'rd')
	else:
		print(str(num)+'th')
"""
output:
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
"""

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章