知识点:
使用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() + ".")