Python编程:从入门到实践第二版答案(第五章)

这个博客展示了如何使用Python中的条件语句(if-else, if-elif-else)根据变量值执行不同操作。内容包括根据alien_color判断得分、根据年龄划分阶段、检查喜爱水果列表、识别用户名、处理用户列表以及检查用户名可用性。此外,还展示了如何使用for循环遍历列表并打印数字的序数形式。
摘要由CSDN通过智能技术生成

5-3

alien_color = 'green'
if alien_color == 'green':
   print("You get five points")

alien_color = 'yellow'
if alien_color == 'green':
   print("You get five points")

 5-4

alien_color = 'green'
if alien_color == 'green':
   print("You get five points")
else:
    print("You get ten points")


alien_color = 'yellow'
if alien_color == 'green':
   print("You get five points")
else:
    print("You get ten points")

5-5

alien_color = 'red'
if alien_color == 'green':
   print("You get five points")
elif alien_color == 'yellow':
    print("You get ten points")
else:
    print("You get 15 points")

5-6

age = 20
if age < 2:
   print("You are infant")
elif age < 4:
    print("You are baby")
elif age < 13:
    print("You are child")
elif age < 20:
    print("You are adolescent")
elif age <65:
    print("You are adult")
else:
    print("You are elder")

5-7

favorite_fruits = ['apple', 'banana', 'orange']
if 'watermelon' in favorite_fruits:
    print("You really like watermelon")
if 'apple' in favorite_fruits:
    print("You really like apple")
if 'grape' in favorite_fruits:
    print("You really like grape")
if 'banana' in favorite_fruits:
    print("You really like banana")
if 'orange' in favorite_fruits:
    print("You really like orange")

 5-8

names = ['day', 'wzy', 'lxd', 'admin', 'zxy']
for name in names:
    if name == 'admin':
        print("Hello admin,would you like to see a status report?")
    else:
        print(f"Hello {name},thank you for logging in again.")

5-9

names = []
if names:
    for name in names:
        if name == 'admin':
            print("Hello admin,would you like to see a status report?")
        else:
            print(f"Hello {name},thank you for logging in again.")
else:
    print("We need to find some users!")

5-10

current_users = ['day', 'wzy', 'lxd', 'admin', 'zxy']
t = []
for names in current_users:
    t.append(names.lower())
new_users = ['day', 'wzy', 'aab', 'bbc', 'cnn']
for new_name in new_users:
    if new_name in t:
        print("You should change your name!")
    else:
        print("Your name is available.")

5-11

numbers = range(1, 10)
for number in numbers:
    if number == 1:
        print(f"{number}st")
    elif number == 2:
        print(f"{number}nd")
    elif number ==3:
        print(f"{number}rd")
    else:
        print(f"{number}th")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机务猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值