从练习4-11开始。
练习4-11:
pizzas = ['黄瓜','茄子','西红柿']
friend_pizzas = pizzas[:]
pizzas.append('白菜')
friend_pizzas.append('辣椒')
print(f"My favorite pizzas are:{pizzas}")
print(f"My friend's favorite pizzas are:{friend_pizzas}")
for pizza in pizzas:
print(pizza)
print("\n")
for friend_pizza in friend_pizzas:
print(friend_pizza)
练习4-12:
略
练习4-13:
foods = ('饺子','面条','油条','包子','豆浆')
for food in foods:
print(food)
print('\n')
foods = ('牛肉','羊肉','油条','包子','豆浆')
for food in foods:
print(food)
练习4-14:略
练习4-15:略
练习5-1
car = 'subaru'
print("Is car == 'subaru'? I predict True.")
print(car == 'subaru')
print("\nIs car == 'audi'? I predict False.")
print(car == 'audi')
练习5-2 略
练习5-3
alien_color = "green"
if alien_color == "green":
print("获得5分")
if alien_color == "yellow":
print("获得10分")
练习5-4
alien_color = "green"
if alien_color == "green":
print("获得5分")
else:
print("获得10分")
练习5-5
alien_color = "green"
if alien_color == "green":
print("获得5分")
elif alien_color == "yellow":
print("获得10分")
else:
print("获得15分")
alien_color = "yellow"
if alien_color == "green":
print("获得5分")
elif alien_color == "yellow":
print("获得10分")
else:
print("获得15分")
alien_color = "red"
if alien_color == "green":
print("获得5分")
elif alien_color == "yellow":
print("获得10分")
else:
print("获得15分")
练习5-6
age = 21
if age < 2:
print("这个人是婴儿。")
elif age >=2 and age < 4:
print("这个人是幼儿。")
elif age >= 4 and age < 13:
print("这个人是儿童。")
elif age >= 13 and age < 20:
print("这个人是青少年。")
elif age >= 20 and age < 65:
print("这个人是成年人。")
else:
print("这个人是老年人。")
练习5-7
favorite_fruits = ['苹果','香蕉','梨']
if '桃' in favorite_fruits:
print(f"You really like 桃!")
if '苹果' in favorite_fruits:
print(f"You really like 苹果!")
if '草莓' in favorite_fruits:
print(f"You really like 草莓!")
if '香蕉' in favorite_fruits:
print(f"You really like 香蕉!")
if '梨' in favorite_fruits:
print(f"You really like 梨!")
练习5-8
users = ['admin','小刘','小张','小王','小李']
for user in users:
if user == 'admin':
print(f"Hello {user}, would you like to see a status report?")
else:
print(f"Hello {user}, thank you for logging in again.")
练习5-9
users = []
if users:
for user in users:
if user == 'admin':
print(f"Hello {user}, would you like to see a status report?")
else:
print(f"Hello {user}, thank you for logging in again.")
else:
print("We need to find some users!")
练习5-10
current_users = ['Liu','Yang','Wang','Zhang','Ma']
new_users = ['Liu','Yang','Li','Gong','Zhao']
b_current_users =[]
for current_user in current_users:
b_current_users.append(current_user.lower())
for new_user in new_users:
if new_user.lower() in b_current_users:
print("已被使用。")
else:
print("未被使用。")
练习5-11
digits = list(range(1,10))
for digit in digits:
if digit == 1:
print(f"{digit}st")
elif digit == 2:
print(f"{digit}nd")
elif digit == 3:
print(f"{digit}rd")
else:
print(f"{digit}th")
练习5-12 略
练习5-13 略