《Python编程 从入门到实践》第五章习题选做

本文通过几个实战案例展示了Python中条件语句的应用,包括不同情况下外星人击杀得分的判断、根据不同年龄判断人生阶段、针对管理员的特殊问候、检查用户名是否已存在以及将数字转换为对应的序数形式。

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

5-3 外星人颜色#1

alien_color = 'green'
if alien_color == 'green':
    print("You have killed " + alien_color + " alien" + " and  got 5 points!")

alien_color = 'red'
if alien_color == 'green':
    print("You have killed " + alien_color + " alien" + " and  got 5 points!")

输出:

You have killed green alien and  got 5 points!

5-4 外星人颜色#2

alien_color = 'green'
if alien_color == 'green':
    print("You have killed " + alien_color + " alien" + " and  got 5 points!")
else:
    print("You have killed " + alien_color + " alien" + " and  got 10 points!")

alien_color = 'red'
if alien_color == 'green':
    print("You have killed " + alien_color + " alien" + " and  got 5 points!")
else:
    print("You have killed " + alien_color + " alien" + " and  got 10 points!")

输出:

You have killed green alien and  got 5 points!
You have killed red alien and  got 10 points!

5-5 外星人颜色#3

alien_color = 'green'
if alien_color == 'green':
    print("You have killed " + alien_color + " alien" + " and  got 5 points!")
elif alien_color == 'yellow':
    print("You have killed " + alien_color + " alien" + " and  got 10 points!")
else:
    print("You have killed " + alien_color + " alien" + " and  got 15 points!")

alien_color = 'yellow'
if alien_color == 'green':
    print("You have killed " + alien_color + " alien" + " and  got 5 points!")
elif alien_color == 'yellow':
    print("You have killed " + alien_color + " alien" + " and  got 10 points!")
else:
    print("You have killed " + alien_color + " alien" + " and  got 15 points!")

alien_color = 'red'
if alien_color == 'green':
    print("You have killed " + alien_color + " alien" + " and  got 5 points!")
elif alien_color == 'yellow':
    print("You have killed " + alien_color + " alien" + " and  got 10 points!")
else:
    print("You have killed " + alien_color + " alien" + " and  got 15 points!")

输出:

You have killed green alien and  got 5 points!
You have killed yellow alien and  got 10 points!
You have killed red alien and  got 15 points!

5-6 人生的不同阶段

age = 34
if age < 2:
    print("You are a baby.")
elif age >= 2 and age < 4:
    print("You are toddling.")
elif age >= 4 and age < 13:
    print("You are a child.")
elif age >= 13 and age < 20:
    print("You are a teenager.")
elif age >= 20 and age < 65:
    print("You are an adult.")
else:
    print("You are an old man.")

输出:

You are an adult.

5-8 以特殊方式跟管理员打招呼

users = ['Mike', 'John', 'admin', 'Amy', 'Ten']
for user in users:
    if user == 'admin':
        print("Hello " + user + ", would you like to see a status report?")
    else:
        print("Hello " + user + ", thank you for logging in again.")

输出:

Hello Mike, thank you for logging in again.
Hello John, thank you for logging in again.
Hello admin, would you like to see a status report?
Hello Amy, thank you for logging in again.
Hello Ten, thank you for logging in again.

5-10 检查用户名

current_users = ['Mike', 'John', 'admin', 'Amy', 'Ten']
new_users = ['Sarah', 'Newton', 'John', 'Ten', 'Hua']
lower_users = []
for user in current_users:
    lower_users.append(user.lower())
for user in new_users:
    if user.lower() in lower_users:
        print("This name has been used, please enter another username.")
    else:
        print("The name has not been used yet.")

输出:

The name has not been used yet.
The name has not been used yet.
This name has been used, please enter another username.
This name has been used, please enter another username.
The name has not been used yet.

5-11 序数

nums = list(range(1, 10))
for num in nums:
    if num == 1:
        print(str(num) + "st")
    elif num == 2:
        print(str(num) + "nd")
    elif num == 3:
        print(str(num) + "rd")
    else:
        print(str(num) + "th")

输出:

1st
2nd
3rd
4th
5th
6th
7th
8th
9th
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值