Python 3 《if》入门练习

#!/usr/bin/env python
# -*- coding:utf-8 -*- 
# author: Christal date: 2021/10/31

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

# 判断中且用and, 或者用or
age0=18
age1=20
if(age0>15) and (age1>20):
    print("True")
else:
    print('False')

#检查特定值是否包含在列表中
requested_toppings = ['mushrooms', 'onions', 'pineapple']
'mushrooms' in requested_toppings #运行后返回TRUE

#检查特定值是否不包含在列表中
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
    print(user.title() + ", you can post a response if you wish.")

#if-elif-else
age=12
if age<4:
    print('Your admission cost is $0.')
elif age<18:
    print('Your admission cost is $5.')
else:
    print('Your admission cost is $10.')

#做如下修改,更实用 (易修改,易移植)
age=22
if age < 4:
    price = 0
elif age < 18:
    price = 5
else:
    price =10
print("Your admission cost is $" + str(price) + ".")
# 也可以不用else
age=22
if age < 4:
    price = 0
elif age < 18:
    price = 5
elif age>= 18:
    price=10
print("Your admission cost is $" + str(price) + ".")

#只想执行一个代码块,使用if-elif-else结构;如果要运行多个代码块,就使用一系列独立的if语句
requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
    print("Adding mushrooms.")
if 'pepperoni' in requested_toppings:
    print("Adding pepperoni.")
if 'extra cheese' in requested_toppings:
    print("Adding extra cheese.")
print("\nFinished making your pizza!")

#缺少配料green peppers
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings:
    if requested_topping == 'green peppers':
        print("Sorry, we are out of green peppers right now.")
    else:
        print("Adding" + requested_topping +".")
print("\nFinished making your pizza!")

# 不添加任何东西
requested_toppings=[]
#requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
if requested_toppings:
    for requested_topping in requested_toppings:
        if requested_topping == 'green peppers':
            print("Sorry, we are out of green peppers right now.")
        else:
            print("Adding " + requested_topping  + ". ")
    print("\nFinished making your pizza!")
else:
    print("Are you want a plain pizza?")

#判断客户要求是否满足
available_toppings = ['mushrooms', 'olives', 'green peppers','pepperoni',
                      'pineapple', 'extra cheese']
requested_toppings = ['mushrooms', 'french fries', 'extra cheese'] # requested_toppings=[]
for requested_topping in requested_toppings:
    if requested_topping in available_toppings:
        print("Adding " + requested_topping +'.')
    else:
        print("Sorry, we do not have "+ requested_topping+ '.')
    print("\nFinished making your pizza!")
print("Are you want a plain pizza?")

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

清韵逐梦

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

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

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

打赏作者

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

抵扣说明:

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

余额充值