标签:else if语句 变量 运算 不同 测试的 遇到 you ann
cars = [‘audi‘,‘bmw‘,‘subaru‘,‘toyota‘] for car in cars: if car == ‘bmw‘: print(car.upper()) else: print(car.title()) #结果如下: #Audi #BMW #Subaru #Toyota
大多数条件测试将一个变量的当前值同特定值进行比较。最简单的条件测试检查变量的值是否与特定值相等。
car = ‘bmw‘ car == ‘bmw‘ #结果如下: #True car = ‘audi‘ car == ‘bmw‘ #结果如下: #False
在Python中检查是否相等时区分大小写。例如,两个大小写不同的值被视为不相等。
car = ‘Audi‘ car == ‘audi‘ #结果如下: #False car = ‘Audi‘ car.lower() == ‘audi‘ #结果如下: #True
requested_topping = ‘mushrooms‘ if requested_topping != ‘anchovies‘: print("Hold the anchovies!") #结果如下: #Hold the anchovies!
age = 18 age ==18 #结果如下: #True answer = 17 if answer != 42: print("That is not the correct answer.Please try angin!") #结果如下: #That is not the correct answer.Please try angin! age = 19 age < 12 age <= 21 age > 21 age >= 21
两个条件都为True,整个表达式为True;如果至少一个测试没有通过,整个表达式就为False。
age_0 = 22 age_1 = 18 age_0 >=21 and age_1 >=21 #结果如下: #False age_1 = 22 age_0 >=21 and age_1 >=21 #结果如下: #True
至少有一个条件为True,整个表达式就为True。
age_0 = 22 age_1 = 18 age_0 >=21 and age_1 >=21 #结果如下: #True age_0 = 18 age_0 >=21 and age_1 >=21 #结果如下: #False
requested_toppings = [‘mushrooms‘,‘onions‘,‘pineapple‘] mushrooms in requested_toppings #结果如下: #True pepperoni in requested_toppings #结果如下: #False
4.2.7 检查特定值是否不包含在列表中(not in)
banned_user = [‘andrew‘,‘carolina‘,‘david‘] user = ‘marie‘ if user not in banned_user: print(f"{user.title()},you can post a response if you wish.") #结果如下: #marie,you can post a response if you wish.
game_active = True
can_edit = False
if conditional_test:
do something
age = 19 if age >= 18: print("You are old enough to vote!") print("Have you registered to vote yet?") #结果如下: #You are old enough to vote! #Have you registered to vote yet?
age = 17 if age >= 18: print("You are old enough to vote!") print("Have you registered to vote yet?") else: print("Sorry,you are to young to vote.") print("Please register to vote as soon as you turn 18!") #结果如下: #Sorry,you are to young to vote. #Please register to vote as soon as you turn 18!
Python只执行if-else-else结构中的一个代码块。它依次检查每个条件测试,直到遇到通过了的条件测试。测试通过后,Python将执行紧跟在它后面的代码,并跳过余下的测试。
age = 12 if age < 4: print("Your admission cost is $0.") else age < 18: print("Your admission cost is $25.") else: print("Your admission cost is $40.") #结果如下: #Your admission cost is $25. #为了使代码更简洁 age = 12
if age <4: price = 0 else age < 18: price = 25 else: price = 40 print(f"Your admission cost is ${price}.")
age = 12
if age <4: price = 0 elif age < 18: price = 25 elif age <65: price = 40 else: price = 20 print(f"Your admission cost is ${price}.")
age = 12 if age < 4: price = 0 elif age < 18: price = 25 elif age <65: price = 40 elif age >=65: price = 20 print(f"Your admission cost is ${price}.")
requested_toppings =[‘mushrooms‘,‘extra cheese‘] if ‘mushrooms‘ in requested_toppings: print("add mushrooms.") if ‘pepperoni‘ in requested_toppings: print("add pepperoni.") if ‘extra cheese‘ in requested_toppings: print("add extra cheese.‘‘) print("\nFinished making your pizza!") #结果如下: #add mushrooms. #add extra cheese. Finished making your pizza!
#使用if-elif-else结构,代码将不能正确运行 requested_toppings =[‘mushrooms‘,‘extra cheese‘] if ‘mushrooms‘ in requested_toppings: print("add mushrooms.") elif ‘pepperoni‘ in requested_toppings: print("add pepperoni.") elif ‘extra cheese‘ in requested_toppings: print("add extra cheese.‘‘) print("\nFinished making your pizza!") #结果如下: #add mushrooms. Finished making your pizza!
requested_toppings =[‘mushrooms‘,‘green peppers‘,‘extra cheese‘] for requested_topping in requested_toppings: print("Adding {requested_topping}.") print("\nFinished making your pizza!") #结果如下: #Adding mushrooms. #Adding green peppers. #Adding extra cheese. #Finished making your pizza! #如果披萨店的青椒用完了,该如何处理? 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!") #结果如下: #Adding mushrooms. #Sorry, we are out of green peppers right now. #Adding extra cheese. #Finished making your pizza!
requested_toppings =[‘mushrooms‘,‘green peppers‘,‘extra cheese‘] for requested_topping in requested_toppings: print("Adding {requested_topping}.") print("\nFinished making your pizza!") #结果如下: #Adding mushrooms. #Adding green peppers. #Adding extra cheese. #Finished making your pizza! #如果披萨店的青椒用完了,该如何处理? 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!") #结果如下: #Adding mushrooms. #Sorry, we are out of green peppers right now. #Adding extra cheese. #Finished making your pizza!
在运行for循环前确定列表是否为空很重要。
requested_toppings = [] if requested_toppings: for requested_topping in requested_toppings: print(f"Adding {requested_topping}") print("\nFinished making your pizza!") else: print("Are you sure you want a plain pizza?") #结果如下: #Are you sure you want a plain pizza?
available_toppings = [‘mushrooms‘,‘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("Adding {requested_topping}.") else: print("Sorry,we don‘t have {requested_topping }") print("\nFinished making your pizza!") #结果如下: #Adding mushrooms. #Sorry,we don‘t have french fries. #Adding extra cheese. #Finished making your pizza!
在条件测试的格式设置方面,PEP8提供的唯一建议是,在诸如==、>=、<=等比较运算符两边各添加一个空格。
标签:else if语句 变量 运算 不同 测试的 遇到 you ann
原文地址:https://www.cnblogs.com/nana12138/p/14510377.html