python基础四——if语句

知识点:
使用if语句处理列表,条件。

示例:

cars = ['audi','bmw','subaru','toyota']
for car in cars:
    if car == 'bmw':
        print(car.upper())
    else:
        print(car.title())

检查特定值是否不包含在列表中,可以用not in来代替循环。

banner_users = ['andrew', 'casrolina', 'david']
user = 'maril'
if user not in banner_users:
    print(user.title() + ", you not in quene")

if-else格式语句

age = 12
if age < 4:
    price = 0
elif age < 18:
    price = 5
else:
    price = 10
print("The amission cost is " + str(price) + ".")

如果想执行一个代码块,就使用if-else结构,如果运行多个代码块,就使用一系列独立的if语句。

requested_topping = ['mashrooms','extrachess']
if 'mashrooms' in requested_topping:
    print("Add mushrooms")
if 'paperenio' in requested_topping:
    print("Add paperenio")
if 'extrachess' in requested_topping:
    print("Add extrachess")

使用if语句处理列表

requested_toppings = ['mashrooms','greenpapers', 'extrachess']
for requested_topping in requested_toppings:
    if requested_topping == 'greenpapers':
        print("out")
    else:
        print("Add " + requested_topping.title() + ".")
print("\nFinished making your pizza.")

使用多个列表

requested_toppings = ['mushrooms', 'french']
available_toppings = ['mushrooms','olives']
for requested_topping in requested_toppings:
    if requested_topping in available_toppings:
        print("Add " + requested_topping.title() + " .")
    else:
        print("Sorry, I don't have " + requested_topping.title() + ".")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值