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

第五章答案

练习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:

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值