if 语句
5.3.1简单的if语句
最简单的if语句只有一个测试和一个操作
age = 19
if age>=18:
print("You are old enough to vote!")
You are old enough to vote!
5.3.2 if-else语句
经常需要在条件测试通过了时执行一个操作,并在没有通过时执行另一个操作,这里采用if-else语句
age =17
if age>=18:
print("You are old enough to vote!")
else:
print("You can't vote!")
You can't vote!
5.3.3 if-elif-else结构
经常需要检查超过两个的情形,为此可以使用Python提供的if-elif-else结构。具体应用如下述代码所示:
age = 16
if age<4:
print("Your admission cost is $0")
elif age<18:
print("Your admission cost is $5")
else:
print("Your admission cost is $10")
Your admission cost is $5
使用多个elif代码块
age = 72
if age < 4:
print("Your admission cost is $0")
elif age < 18:
print("Your admission cost is $8")
elif age <65:
print("Your admission cost is $10")
else:
print("Your admission cost is $6")
Your admission cost is $6
if age < 4:
price = 0
elif age < 18:
price = 8
elif age < 65:
price = 10
else:
price = 6
print("Your admission cost is $" + str(price)+'.')
Your admission cost is $6.
测试多个条件
if-elif-else 结构功能强大,但仅适用于只有一个条件满足的情况:遇到通过了的测试后,Python就跳过余下的测试。这种行为很好,效率很高,让你能够测试一个特定的事件。
然而,有时候必须检查你关心的所有条件。在这种条件下,应该使用一系列不包含elif和else代码块的简单if语句。有可能有多个条件为True,我们需要将每个条件为True的时候去执行相对应的操作。
requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")
if 'pepperon' in requested_toppings:
print("Adding pepperon.")
if 'extra cheese' in requested_toppings:
print("Adding extra cheeese.")
print("\n Finished making your pizza!")
Adding mushrooms.
Adding extra cheeese.
Finished making your pizza!
5.4 使用if语句处理列表
5.4.1检查特殊元素
requested_toppings = ['mushroom', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings:
print("Adding " + requested_topping + '.')
print('\n Finished making your pizza!')
Adding mushroom.
Adding green peppers.
Adding extra cheese.
Finished making your pizza!
requested_toppings = ['mushroom', '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('\n Finished making your pizza!')
Adding mushroom.
Sorry, we are out of green peppers right now.
Adding extra cheese.
Finished making your pizza!
5.4.2确定列表不是空的
到目为止,对于处理的每个列表都做了一个简单的假设,即假设它们都至少包含一个元素,我们在for循环之前需要判断列表是否为空列表这个很重要。
requested_toppings = []
if requested_toppings:
for requested_topping in requested_toppings:
print('Adding '+ requested_topping + '.')
print('\n Finshed making your pizza!')
else:
print('Are you sure want a plain pizza ?')
Are you sure want a plain pizza ?
我们在用for循环对列表查询的时候首先要查询一下列表是否为空然后再进行操作。Python将在列表至少包含一个元素时返回True,并在列表为空时返回False
使用多个列表
available_toppings = ['mushrooms', 'olives', 'green peppers',
'pepperon', 'pineapple', 'extra chesse']
requested_toppings = ['mushrooms', 'french fries', 'extra chesse']
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('\n Finished making your pizza!')
Adding mushrooms.
Sorry, we don't havefrench fries.
Adding extra chesse.
Finished making your pizza!