if语句

if语句

Python中,if语句让你能够检查程序的当前状态,并据此采取相应的措施。

5-1

coffers = ['拿铁','美式','卡布奇诺','coco' ]
for coffer in coffers:
    if coffer == '拿铁':
        print(coffer)
    else:
        print(coffer)

5 − 2 5-2 52

message = 'aAshovemend'
if message.lower() != 'aashovemend':
    print("hellow python")
else:
    print(message)
aAshovemend
num = 22
if num == 24:
    print("please try again!")
else:
    num !=23 or num<=23
    print("jiejinle")
jiejinle

num = ['a','s','w']
if 'a' in num:
    print('duide')
if 'q' not in num:
    print('you are right')
duide
you are right
request = ['a','b','d']
'a' in request
True

5 − 3 5-3 53

aline_color = 'green'
if aline_color == 'green':
    print("恭喜你获得五点积分")
恭喜你获得五点积分

5 − 4 5-4 54

aline_color = 'red'
if aline_color == 'green':
    print("恭喜你获得五点积分")
else:
    print("恭喜你获得十点积分")
恭喜你获得十点积分

5 − 5 5-5 55

aline_color = 'red'
if aline_color == 'green':
    print("5分")
elif aline_color == 'yello':
    print("10fen")
else:
    print("15fen")
15fen

5 − 6 5-6 56

age = 20
if age < 2:
    print("婴儿")
elif age < 4:
    print("学习走路阶段")
elif age < 13:
    print("儿童")
elif age < 20:
    print("青少年")
elif age < 65:
    print("成年人")
else:
    print("老人")
成年人

5 − 7 5-7 57

favorite_fruits = ['orange','dragon fruit','apple']
if 'orange' in favorite_fruits:
    print("You really like orange")
if 'dragon fruit' in favorite_fruits:
    print("You really like drage")
You really like orange
You really like drage

5 − 8 5-8 58

names = ['admin','reic','lucy','jone','nade']
for name in names:
    if name == 'admin':#赋值和等号
        print("Hello " + name.title() + '.')
    else:
        print("Hello " + name.title() + ",thank you for logging in again.")
Hello Admin.
Hello Reic,thank you for logging in again.
Hello Lucy,thank you for logging in again.
Hello Jone,thank you for logging in again.
Hello Nade,thank you for logging in again.

5 − 9 5-9 59

names = []
if names:
    for nume in names:
        print("have " + name)
else:
      print("We need to find some users")
for name in names:
    if name == 'admin':
        print("Hello " + name.title() + '.')
    else:
        print("Hello " + name.title() + ",thank you for logging in again.")
        
We need to find some users

5 − 10 5-10 510

current_users = ['A','b','c','d','e','f','g']
new_users = ['h','i','g','A','r']
for new in new_users:
    if new in current_users:
        print(new + "已经被使用")
    elif new.lower() == 'a':
        print("yibeishiyong")
    else:
        print(new + "没有使用")
        
        
h没有使用
i没有使用
g已经被使用
A已经被使用
r没有使用

5 − 11 5-11 511

nums = list(range(1,10))
for num in nums:
    if str(num) == '1':
        print(str(num) + "st")
    if str(num) == '2':
        print(str(num) + "nd")
        
    
1st
2nd
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值