python基础之条件语句
检查相等和不等
多个检查条件
age1 = 22
age2 = 19
s1 = age1 > 21 and age2 > 19
print(s1)
s2 = age1 > 21 or age2 > 19
print(s2)
检查是否包含在列表内
requested_toppings = ['mushrooms','onions','pineapple']
s1 = 'mushrooms' in requested_toppings
print(s1)
s2 = 'pepperoni' in requested_toppings
print(s2)
检查不包含
requested_toppings = ['mushrooms','onions','pineapple']
s1 = 'mushrooms' not in requested_toppings
print(s1)
s2 = 'pepperoni' not in requested_toppings
print(s2)
if语句
age = 19
if age >= 18:
print('You are old enough to vote')
if age >= 20:
print('is no print')
if-elif-else语句
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
else:
price = 5
print('Your admission cost is $' + str(price) + '.')
检查特殊元素
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 + '.')
requested_toppings= []
if requested_toppings:
for requested_topping in requested_toppings:
print('Adding ' + requested_topping + '.')
else:
print('Are you sure you want a plain pizza?')
available_toppings = ['mushrooms','olives','green peppers','pepperoni']
requested_toppings= ['mushrooms','french fries','green peppers']
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 + '.')
本文来自博客园,作者:Harry_666,转载请注明原文链接:https://www.cnblogs.com/harry66/p/13764499.html