python从入门到实践-5章if语句

#!/user/bin/env python

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

# == !=
# <= >=
# and or
# in not in
# True False
# if-else
# if-elif-elif-else # 选择一个执行
requesed_toppings = ['mushrooms','green peppers','extra cheese']
for requesed_topping in requesed_toppings:
if requesed_topping == 'green peppers':
print("Sorry,we are out of green peppers now.")
else:
print("Addin" + requesed_topping + ".")
print(' Finished making your pizza!')


# 确定列表不是空
requesed_toppings = []
if requesed_toppings:
for requesed_topping in requesed_toppings:
print("Adding" + requesed_topping + ".")
print(" Finished making your pizza!")
else:
print("Are you sure you want a plain pizza?")

原文地址:https://www.cnblogs.com/vwei/p/9879982.html