《Python编程:从入门到实践》习题答案——第5章 if语句

# 5-1 条件测试:
car = 'subaru'
print(car == 'subaru')
print(car == 'audi')

num = 8
print(num > 10)
print(num < 5)

# 5-2 更多的条件测试:
# 1.检查两个字符串相等和不等
str1 = 'kan'
str2 = 'can'
print(str1 == str2)
print(str1 != str2)

# 2.使用函数lower()的测试
str1= "aaaaa"
str2 = "AAAAA"
print(str2.casefold())             
print(str2.lower())                
print(str1 == str2.casefold())
print(str1 == str2.lower())

# 3.检查两个数字相等、不等、大于、小于、大于等于和小于等于
num1 = 15
num2 = 33
print(num1 == num2)
print(num1 != num2)
print(num1 > num2)
print(num1 < num2)
print(num1 >= num2)
print(num1 <= num2)

# 4.使用关键字and和or的测试
num = 22
if (num > 20) and (num < 30):
    print(True)
else:
    print(False)

num = 66
if (num < 33) or (num > 55):
    print(True)
else:
    print(False)

# 5.测试特定的值是否包含在列表中
list = [1, 2, 3, 4, 5, 6]
if 1 in list:
    print(True)
else:
    print(False)

# 6.测试特点的值是否未包含在列表中
list = [1, 2, 3, 4, 5, 6]
if 10 not in list:
    print(True)
else:
    print(False)



# 5-3 外星人颜色:
print("----------版本1----------------")
alien_color = ['green', 'yellow', 'red']
alien_color_dis = 'green'
if alien_color_dis in alien_color:
    print("获得5个点\n")
print("----------版本2----------------")
alien_color = ['yellow', 'red']
alien_color_dis = 'green'
if alien_color_dis in alien_color:
    print("获得5个点")

# 5-4 外星人颜色2
print("----------版本1----------------")
alien_color = ['green', 'yellow', 'red']
alien_color_dis = 'green'
if alien_color_dis in alien_color:
    print("获得5个点")
else:
    print("获得10个点")

print("\n----------版本2----------------")
alien_color = ['yellow', 'red']
alien_color_dis = 'green'
if alien_color_dis in alien_color:
    print("获得5个点")
else:
    print("获得10个点")

# 5-5 外星人颜色3
print("----------版本1----------------")
alien_color = ['green', 'yellow', 'red']
if 'green' in alien_color:
    print("获得5个点")
elif 'yellow' in alien_color:
    print("获得10个点")
elif 'red' in alien_color:
    print("获得15个点")

print("\n----------版本2----------------")
alien_color = ['yellow', 'red']
if 'green' in alien_color:
    print("获得5个点")
elif 'yellow' in alien_color:
    print("获得10个点")
elif 'red' in alien_color:
    print("获得15个点")

print("\n----------版本3----------------")
alien_color = ['red']
if 'green' in alien_color:
    print("获得5个点")
elif 'yellow' in alien_color:
    print("获得10个点")
elif 'red' in alien_color:
    print("获得15个点")

# 5-6 人生的不同阶段:
age = int(input("请输入您的年龄:"))

if age < 2:
    print("他是个婴儿。")

elif 2 <= age < 4:
    print("他正在蹒跚学步。")

elif 4 <= age < 13:
    print("他是儿童。")

elif 13 <= age < 20:
    print("他是青少年。")

elif 20 <= age < 65:
    print("他是成年人。")

else :
    print("他是老年人。")

# 5-7 喜欢的水果:
favorite_fruits = ['apple', 'banana', 'orange']
if 'apple' in favorite_fruits:
    print(f"You really like {favorite_fruits[0]}")
if 'banana' in favorite_fruits:
    print(f"You really like {favorite_fruits[1]}")
if 'orange' in favorite_fruits:
    print(f"You really like {favorite_fruits[2]}")
if 'pear' in favorite_fruits:
    print(f"You really like pear")
if 'cherry' in favorite_fruits:
    print(f"You really like cherry")


# 5-8 以特殊方式跟管理员打招呼:
names = ['bob', 'admin', 'alice', 'jake', 'licy']
for name in names:
    if name == 'admin':
        print("Hello admin, would you like to see a status report?")
    else:
        print("Hello " + name.title() + ", thank you for logging in again.")

# 5-9 处理没有用户的情形:
names = ['bob', 'admin', 'alice', 'jake', 'licy']
if names:   
    for name in names:
        if name == 'admin':
            print("Hello admin, would you like to see a status report?")
        else:
            print("Hello " + name + ", thank you for logging in again.")
else:      
    print("We need to find some users!")
print(names)
names.remove('bob')
names.remove('admin')
names.remove('alice')
names.remove('jake')
names.remove('licy')    
print(names)

# 5-10 检查用户名:
current_users = ['bob', 'admin', 'alice', 'jake', 'licy']
new_users = ['anlen', 'Alice', 'admin', 'demo', 'nomi']
for new_user in new_users:
    if new_user.lower() in current_users:
        print(f"{new_user}该用户名已被使用,请换一个用户名。")
    else:
        print(f"{new_user}该用户名未被使用。")

# 5-11 序数:
lists = list(range(1, 10))
for list in lists:
    if list == 1:
        print('1st')
    elif list == 2:
        print('2nd')
    elif list == 3:
        print('3rd')
    else:
        print(f"{list}th")

# 5-12 设置if语句的格式

# 5-13 自己的想法

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值