Python编程:从入门到实践 第五章习题解析

本文通过多个实例演示了Python中条件判断的应用,包括字符串比较、数值判断、列表元素检查及用户登录验证等。通过这些示例,读者可以更好地理解如何在实际编程中运用条件语句。

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

已经标有题号了,就不额外多讲了。

#5-2
str1 = "abc"
str2 = "Abc"
str3 = "ABC"
n1   = 10
n2   = 11

print(str1 == str2)
print(str1 == str2.lower())
print(n1   <  n2)
print(n1   <=  n2)
print(n1   ==  n2)
print(n1   >  n2)
print(n1   >=  n2)
print(n1 <= n2 and str1 == str3.lower())
print(n1 < n2 or str1 == str2)
print('a' in str2)
print('a' not in str1) #regard string as a list of chars


#5-3
colors = ["green","yellow","red"]
alien_color1 = colors[1]
alien_color2 = colors[0]

if alien_color1 == "green":
    print("5 poionts!")
if alien_color2 == "green":
    print("5 poionts!")

#5-4
if alien_color1 == "green":
    print("5 poionts!")
else: print("10 points!")
if alien_color2 == "green":
    print("5 poionts!")
else: print("10 points!")

#5-5
alien_color3 = colors[2]
if alien_color1 == "green":
    print("5 poionts!")
elif alien_color1 == "yellow":
    print("10 poionts!")
elif alien_color1 == "red":
    print("15 poionts!")
if alien_color2 == "green":
    print("5 poionts!")
elif alien_color2 == "yellow":
    print("10 poionts!")
elif alien_color2 == "red":
    print("15 poionts!")
if alien_color3 == "green":
    print("5 poionts!")
elif alien_color3 == "yellow":
    print("10 poionts!")
elif alien_color3 == "red":
    print("15 poionts!")

#5-6
age = 19
if age < 2:
    print("You are a baby~")
elif age <4:
    print("You are learing walking.")
elif age <13:
    print("You are a kid.")
elif age <20:
    print("You are a teenager.")
elif age <65:
    print("You are an adult.")
elif age >=65:
    print("You are an elder.")

#5-7
favorite_fruits = ["orange","pear","apple"]
if "orange" in favorite_fruits:
    print("You realy like oranges!")
if "pear" in favorite_fruits:
    print("You realy like pears!")
if "apple" in favorite_fruits:
    print("You realy like apples!")
if "grasp" in favorite_fruits:
    print("You realy like grasps!")
if "bananna" in favorite_fruits:
    print("You realy like banannas!")


#5-8
users = ["A",'B','C','D','admin']
for user in users:
    if user == "admin":
        print("Hello admin, would you like to see a status report?")
    else:
        print("Hello,",user,"thank you for logging in again")

#5-9
users = []
if users:
    for user in users:
        if user == "admin":
            print("Hello admin, would you like to see a status report?")
        else:
            print("Hello,",user,"thank you for logging in again")

else:
    print("We need to find some users!")

#5-10
current_users = ["A",'B','C','D','admin']
new_users = ["admin","B","E","F","GH"]
if new_users:
    for new_user in new_users:
        if new_user.lower() in [u.lower() for u in current_users]:
            print("NAME HAS BEEN USED. TRY another one.")
        else :
            print("NAME HAS NOT BEEN USED.")

输出:

===================== RESTART: D:/program/Python/第五章.py =====================
False
True
True
True
False
False
False
True
True
False
False
5 poionts!
10 points!
5 poionts!
10 poionts!
5 poionts!
15 poionts!
You are a teenager.
You realy like oranges!
You realy like pears!
You realy like apples!
Hello, A thank you for logging in again
Hello, B thank you for logging in again
Hello, C thank you for logging in again
Hello, D thank you for logging in again
Hello admin, would you like to see a status report?
We need to find some users!
NAME HAS BEEN USED. TRY another one.
NAME HAS BEEN USED. TRY another one.
NAME HAS NOT BEEN USED.
NAME HAS NOT BEEN USED.
NAME HAS NOT BEEN USED.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值