[python] if statement

cars = ['audi','bmw','subaru','toyota']

for car in cars:
	if car == 'bmw':
		print(car.upper())
	else:
		print(car.title())

#检查多个条件
age_0 = 22
age_1 = 18
#and
print(age_0 >=21 and age_1>=21)
#or
print(age_0 >=21 or age_1>=21)

#检查特定的值是不是在已知列表中
requested_toppings = ['mushrooms','onions','pineapple']
print('mushrooms' in requested_toppings)

banned_users = ['andrew','carolina','david']
user = 'marie'

if user not in banned_users:
	print(f"{user.title()}, you can post a response if you wish.")

age = 12
if age < 4:
	print("Your admission cost is 0$")
elif age<18:
	print("Your admission cost is 25$")
else:
	print("Your admission cost is 40$")

#测试多个条件
requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
	print("Adding mushrooms.")
if "pepperoni" in requested_toppings:
	print("Adding pepperoni.")
print("\nFinished making your pizza.")

#确认列表不是空的
requested_toppings = []

if requested_toppings:
	for requested_topping in requested_toppings:
		print(f"Adding {requested_toppings}")
	print("Finished making your pizza")
else:
	print("Are you sure you want a plain pizza?")

available_toppings = ['mushrooms', 'olives', 'green peppers', 'pepperoni',
                      'pineapple','extra cheese']
requested_toppings = ['mushrooms', 'french fries', 'extra cheese']
for requested_topping in requested_toppings:
	if requested_topping in available_toppings:
		print(f"Adding {requested_topping}")
	else:
		print(f"Sorry, we don't have {requested_topping}")


 

Guess you like

Origin blog.csdn.net/qq_39696563/article/details/116754345