python从入门到实践-课后练习-第五章

  • 5-3 外星人颜色#1:假设在游戏中刚射杀了一个外星人,请创建一个名为alien color的变量,并将其设置为green'、yellow'或'red。
  1. 编写一条f语句,检查外星人是否是绿色的;如果是,就打印一条消息,指出玩家获得了5个点。
  2. 编写这个程序的两个版本,在一个版本中上述测试通过了,而在另一个版本未通过(未通过测试时没有输出 )。
alien_color = ['green', 'red']
for i in alien_color:
    if i == 'green':
        print('you have got 5 score')
    else:
        print('nothing')
  • 5-4外星人颜色#2:像练习5-3那样设置外星人的颜色,并编写一个if-else结山
  1. 如果外星人是绿色的,就打印一条消息,指出玩家因射杀该外星人获得了5个点
  2. 如果外星人不是绿色的,就打印一条消息,指出玩家获得了10个点
  3. 编写这个程序的两个版本,在一个版本中执行f代码块,而在另一个版本中执行else代码块。
alien_colour = 'red'
if alien_colour == 'green':
    print('you got 5!')
elif alien_colour != 'green':
    print('you got 10')

  • 5-5外星人颜色#3:将练习5-4中的if-else结构改为if-elif-else结构
  1. 如果外星人是绿色的,就打印一条消息,指出玩家获得了5个点
  2. 如果外星人是黄色的,就打印一条消息,指出玩家获得了10个点
  3. 如果外星人是红色的,就打印一条消息,指出玩家获得了15个点。
  4. 编写这个程序的三个版本,它们分别在外星人为绿色、黄色和红色时打印一条消息。
alien_colour = 'red'
if alien_colour == 'green':
    print('you got 5')
elif alien_colour == 'yellow':
    print('you got 10')
else:
    print('you got 15')
  • 5-6人生的不同阶段:设置变量age的值,再编写一个if-elif-else结构,根据ge的值判断处于人生的哪个阶段
  1. 如果一个人的年龄小于2岁,就打印一条消息,指出他是婴儿
  2. 如果一个人的年龄为2(含)~4岁,就打印一条消息,指出他正蹒跚学步
  3. 如果一个人的年龄为4(含)~13岁,就打印一条消息,指出他是儿童。
  4. 如果一个人的年龄为13(含)~20岁,就打印一条消息,指出他是青少年
  5. 如果一个人的年龄为20(含)-65岁,就打印一条消息,指出他是成年人
  6. 如果一个人的年龄超过65(含)岁,就打印一条消息,指出他是老年人
age = 60
if age < 2:
    print('婴儿')
elif age < 4:
    print('学步')
elif age < 13:
    print('儿童')
elif age < 20:
    print('青年')
elif age < 65:
    print('成年')
else:
    print('老年')
  • 5-7喜欢的水果:创建一个列表,其中包含你喜欢的水果,再编写一系列独立的if语句,检查列表中是否包含特定的水果。
  1. 将该列表命名为favorite fruits,并在其中包含三种水果
  2. 编写5条if语句,每条都检查某种水果是否包含在列表中,打印一条信息,“I really like bananas”
fruit = ['cherry', 'banana', 'orange']
if 'apple' in fruit:
    print('I really like apple')
if 'lemon' in fruit:
    print('I really like lemon')
if 'banana' in fruit:
    print('I really like banana')
if 'peach' in fruit:
    print('I really like peach')
if 'cherry' in fruit:
    print('I really like cherry')

  • 5-8以特殊方式跟管理员打招呼:创建一个至少包含5个用户名的列表,且其中一个用户名为'admin。想象你要编写代码,在每位用户登录网站后都打印一条问候消息。遍历用户名列表,并向每位用户打印一条问候消息。
  1. 如果用户名为'admin',就打印一条特殊的问候消息,如“Hello admin would you like to see a status report?”
  2. 否则,打印一条普通的问候消息,如“Hello Eric,thank you for logging in again
names = ['Eric', 'Bill', 'Ryan', 'Billy', 'Admin']
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处理没有用户的情形:在为完成练习5-8编写的程序中,添加一条if语句,检查用户名列表是否为空。
  1. 如果为空,就打印消息“We need to find some users!
  2. 删除列表中的所有用户名,确定将打印正确的消息。
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 some new users')
del names[0:5]

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 some new users')
  • 5-10检查用户名:按下面的说明编写一个程序,模拟网站确保每位用户的用户名都独一无二的方式。
  1. 创建一个至少包含5个用户名的列表,并将其命名为 current_users。
  2. 再创建一个包含5个用户名的列表,将其命名为new users,并确保其中有一两个用户名也包含在列表current users中。
  3. 遍历列表new users,对于其中的每个用户名,都检查它是否已被使用。如果是这样,就打印一条消息,指出需要输入别的用户名;否则,打印一条消息,指出这个用户名未被使用
  4. 确保比较时不区分大小写;换句话说,如果用户名john’已被使用,应拒绝用名JOHN。
current_uses = ['Eric', 'Bill', 'Ryan', 'Billy', 'Admin']
new_users = ['Eric', 'willie', 'admin', 'erin', 'ever']
current_uses_lower = [current_use.lower() for current_use in current_uses]
new_users_lower = [new_user.lower() for new_user in new_users]
for i in new_users_lower:
    if i in current_uses_lower:
        print('insert other user name')
    else:
        print('this user name does not be used')
  • 5-11序数:序数表示位置,如lst和2nd。大多数序数都以th结尾,只有1、2和3例外。
  1. 在一个列表中存储数字1~9。
  2. 遍历这个列表。
  3. 在循环中使用一个if-elif-else 结构,以打印每个数字对应的序数。输出内客应为1st、2nd、3rd、4th、5th、6th、7th、8th和gth,但每个序数都独占一行
numbers = list(range(1, 10, 1))
for number in numbers:
    if number == 1:
        print('1st')
    elif number == 2:
        print('2nd')
    elif number == 3:
        print('3rd')
    else:
        print(f'{number}th')

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Charliewyzzzz

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

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

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

打赏作者

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

抵扣说明:

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

余额充值