Python学习日志 - 5.if语句(习题解答)

本文通过多个示例展示了Python中条件语句的使用方法,包括不同颜色外星人的得分规则、年龄判断、检查水果偏好等,帮助读者理解if-elif-else结构的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 5-3
alien_color = "green"
if alien_color == "green":
print("You got 5 point")

alien_color = "red"
if alien_color == "green":
print("You got 5 point")

#5-4
alien_color = "red"
if alien_color == "green":
print("You got 5 point")
else:
print("You got 10 point")

alien_color = "green"
if alien_color == "green":
print("You got 5 point")
else:
print("You got 10 point")

#5-5
alien_color = "green"
if alien_color == "green":
print("You got 5 point")
elif alien_color = "yellow":
print("You got 10 point")
elif alien_color = "red":
print("You got 15 point")

alien_color = "red"
if alien_color == "green":
print("You got 5 point")
elif alien_color = "yellow":
print("You got 10 point")
elif alien_color = "red":
print("You got 15 point")

alien_color = "yellow"
if alien_color == "green":
print("You got 5 point")
elif alien_color = "yellow":
print("You got 10 point")
elif alien_color = "red":
print("You got 15 point")

#5-6
age = 19
if age < 2:
print("He is a baby")
elif age < 4:
print("He is learning to walk")
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 a old man")

#5-7
favorite_fruits = ["apples", "bananas", "oranges"]
if "apples" in favorite_fruits:
print("you really like apples")
if "bananas" in favorite_fruits:
print("you really like bananas")
if "oranges" in favorite_fruits:
print("you really like oranges")
if "mellons" in favorite_fruits:
print("you really like mellons")
if "pears" in favorite_fruits:
print("you really like pears")

#5-8
usernames = ["admin", "bob", "alice", "david", "john"]
for user in usernames:
if user == "admin":
print("Hello {}, would you like to see a status report?".format(user))
else
print("Hello {}, thank you for logging in again".format(user))

#5-9
usernames = ["admin", "bob", "alice", "david", "john"]
usernames = []
if len(usernames) == 0:
print("We need to find some users!")
for user in usernames:
if user == "admin":
print("Hello {}, would you like to see a status report?".format(user))
else
print("Hello {}, thank you for logging in again".format(user))

#5-10
current_users = ["admin", "bob", "alice", "david", "john"]
new_users = ["bob", "john", "cindy", "daicy", "bill"]
for new_user in new_users:
if new_user in current_users:
print("Username {} is already exist, please change another one.".format(new_user))
else:
print("This username is valid")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值