第五章答案
练习5-1:
略
练习5-2:
略
练习5-3:
alien_color = 'green'
if alien_color == 'green':
print("You get 5 score!")
练习5-4:
alien_color = 'green'
if alien_color == 'green':
print("You get 5 score!")
else:
print("You get 10 score!")
练习5-5:
alien_color = 'green'
if alien_color == 'green':
print("You get 5 score!")
elif alien_color == "yellow":
print("You get 10 score!")
else:
print("You get 15 score!")
练习5-6:
age = 23
if age < 2:
print("婴儿")
elif age < 4:
print("幼儿")
elif age < 13:
print("儿童")
elif age < 20:
print("青少年")
elif age < 65:
print("成年人")
else:
print("老年人")
练习5-7:
favorite_fruits = ["apple", "banana", "orange"]
if "apple" in favorite_fruits:
print("apple is one of my favorite fruits")
# 剩余四条类似
练习5-8:
users = ["abc", "def", "ghi", "jkl", "admin"]
for user in users:
if user == "admin":
print("Hello admin, would you like to see a status report?")
else:
print(f"Hello {user}, thank you for logging in again.")
练习5-9:
users = ["abc", "def", "ghi", "jkl", "admin"]
for user in users:
if user == "admin":
print("Hello admin, would you like to see a status report?")
else:
print(f"Hello {user}, thank you for logging in again.")
users = []
if len(users) == 0:
print("You did not enter any users.")
练习5-10:
current_users = ["Alice", "Bob", "John", "Jack", "David"]
new_users = ["LiHua", "JOHN", "Albert", "smith", "alice"]
current_users_lower = []
for user in current_users:
current_users_lower.append(user.lower())
for user in new_users:
if user.lower() in current_users_lower:
print(f"{user} has been registered, please input another user")
else:
print("register successfully!")
练习5-11:
nums = list(range(1, 10))
for num in nums:
if num == 1:
print(f"{num}st")
elif num == 2:
print(f"{num}nd")
elif num == 3:
print(f"{num}rd")
else:
print(f"{num}th")
练习5-12:
略
练习5-13:
略