《python编程从入门到实践》第五章习题答案(if语句)

5-1条件测试

cars=['bmw','audi','toyota']
car='bmw'
#True
print(car == 'bmw')
#False
print(car != 'bmw')
#True
print(car == car)
#False
print(car == car.title())
#True
print(car == 'bmw' or  car == 'audi')
#False
print(car == 'bmw' and car == 'audi')
#True
print(int(len(car)) == int(len('bmw')))
#False
print(int(len(car)) == int(len('audi')))
#True
print('bmw' in cars)
#False
print('audi' not in cars)


5-2更多的条件测试

name_1 = 'louie'
name_3 = 'Louie'
# 字符串比较
print(name_1 == name_3)
print(name_1 == name_3.lower())

num_1 = 5
num_2 = 9
# 数字比较
print(num_1 == num_2)
print(num_1 != num_2)
print(num_1 > num_2)
print(num_1 < num_2)

# 使用 and 和 or 来比较
if num_1 > 5 and num_2 > 5:
    print("This is true")
else:
    print("This is False")

if num_1 > 5 or num_2 > 5:
    print("This is true")
else:
    print("This is False")

names = ['louie', 'link', 'lisa', 'park']
name = 'links'

if name in names:
    print(name + " in the list names")
else:
    print(name + " not in the list names")


5-3外星人颜色1

aline_color='green'
if aline_color=='green':
    print('you got 5 points.')
else:
    print('no')


alien_color = 'red'
if alien_color == 'green':
    print("You got 5 point!~")
else:
    print("no")


5-4外星人颜色2

alien_color = "green"
if alien_color == "green":
    print("You get 5 points.")
else:
    print("You get 10 points.")

5-5外星人颜色3

alien_color='green'
if alien_color =='green':
    print("You get 5 points.")
elif alien_color == 'red':
    print("You get 10 points.")
else:
    print("You get 15 points.")

5-6人生中的不同阶段

age=12
if age < 2:
    print("You are a baby")
elif 2 <= age < 4:
    print("He is making a toddler.")
elif 4 <= age < 13:
    print("You are a child.")
elif 13 <= age < 20:
    print("You are a young man.")
elif 20 <= age < 65:
    print("You are an adult.")
else:
    print("You are an old man.")

age可以更改
不过可以把第一句更改成输入判断,鉴于第五章还未学,第一种答案给的是随意赋值的
可以改为

age = input("输入年龄:")

5-7喜欢的水果

favorite_fruits = ['banana', 'apple', 'orange']
if "apple" in favorite_fruits:
    print("You really like apple!")
if "orange" in favorite_fruits:
    print("You really like orange!")
if "orange" in favorite_fruits:
    print("You really like orange!")
if "milk" in favorite_fruits:
    print("You really like milk!")
else:
    print("The pears not list")

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

name_list = ["bob", "jane", "jack", "admin", "tom"]
for i in name_list:
    if i == "admin":
        print("Hello "+i+", would you like to see a status report?" )
    else:
        print("Hello "+i+", thank you for logging in again" )

5-9处理没有用户的情形

name_list = []
if len(name_list) == 0:
    print("We need to find some users!")
else:
    for i in name_list:
        if i == "admin":
            print("Hello %s, would you like to see a status report?" % i)
        else:
            print("Hello %s, thank you for logging in again" % i)

5-10检查用户名

current_users = ["bob", "jane", "jack", "admin", "tom"]
new_users = ["Bob", "james", "NIck", "JACK", "name"]
for i in new_users:
    if i.lower() in current_users:
        print("%s 已经被使用,请更换用户名" % i)
    else:
        print("%s 没被注册,可以使用" % i)

5-11序数

nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
for num in nums:
    if num == "1":
        print("1st")
    elif num == "2":
        print("2nd")
    elif num == "3":
        print("3rd")
    else:
        prin

5-12设置if语句的格式

5-13自己的想法

学python机器学习欢迎关注公众号
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wakaweika

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

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

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

打赏作者

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

抵扣说明:

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

余额充值