if语句
1.使用if语句处理列表
通过结合使用if语句和列表,可以完成一些有趣的任务:对列表中特定的值做特殊处理;高效地管理不断变化的情形,如餐馆是否还有特定的食材;证明代码在各种情形下都将按预期那样运行
1>检查特殊元素
下面来举例说说关于如何检查列表中的特殊值,并对其做合适的处理。
那就继续使用前面的举例的披萨店吧,这家比萨店在制作比萨时,每添加一种配料都打印一条消息,通过创建一个列表,在其中包含顾客点的配料,并使用一个循环来指出添加到比萨中的配料,可以以极高的效率编写这样的代码:
requested_toppings = ['mushrooms','green peppers','extra cheese']
for requested_topping in requested_toppings:
print("Adding " + requested_topping + ".")
print("\nFinished making your pizza!")
执行结果:
book@www.100ask.org:~/python/L7$ python toppings-1.py
Adding mushrooms.
Adding green peppers.
Adding extra cheese.
Finished making your pizza!
下面问题就来了,如果比萨店的青椒用完了,该如何处理呢?为了妥善地处理这种情况,可在for循环中包含一条if语句:
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("Adding " + requested_topping + ".")
print("\nFinished making your pizza!")
执行结果:
book@www.100ask.org:~/python/L7$ python toppings-1.py
Adding mushrooms .
Adding mushrooms.
Sorry, we are out of green peppers right now.
Adding green peppers.
Adding extra cheese .
Adding extra cheese.
Finished making your pizza!
2>确定列表不是空的
在之前的列表处理中,我们假设的列表都是有值的,但如果列表时空的呢,所以在运行for循环之前,需要检查列表是否为空很重要。
还是之前制作比萨的例子,在制作比萨前检查顾客点的配料列表是否为空。如果列表为空,就像顾客确认他是否要点普通比萨;如果列表不为空,就制作顾客点的比萨:
requested_toppings = []
if requested_toppings:
for requested_topping in requested_toppings:
print("Adding " + requested_topping + " . ")
print("\n Finished making your pizza!")
else:
print("Are you sure you want a plain pizza ? ")
结果呢:
book@www.100ask.org:~/python/L7$ python toppings-1.py
Are you sure you want a plain pizza ?
3>使用多个列表
大部分时间顾客的需求时五花八门的,在煎饼果子的配料方面尤其如此,如果顾客要在煎饼果子中添加熊掌,该怎么办呢?可使用列表和if语句来确定能否满足顾客的要求。
下面举例子看看在摊煎饼时如何拒绝怪异的佐料要求。下面的示例定义了两个列表,其中第一个列表包含煎饼果子大众的佐料,而第二个列表包含顾客点的佐料。这次对于Chinese_savior_crepe中的每一个元素都检查他是否是煎饼果子中大众的佐料。
Chinese_savior_crepe = ['egg', 'Ham sausage', 'chili sauce','Wei Long','chopped green onion','parsley']
custom_request = ['egg','Ham sausage','abalone']
if Chinese_savior_crepe and custom_request:
for requested in custom_request:
if requested in Chinese_savior_crepe:
print("Adding " + requested + " . ")
else:
print("Sorry, we don't have " + requested + " . ")
print("\nFinished making your Chinese savior crepe!")
执行结果:
book@www.100ask.org:~/python/L7$ python Chinese_savior_crepe.py
Adding egg .
Adding Ham sausage .
Sorry, we don't have abalone .
Finished making your pizza!
通过了了几行code,让我们感受带了,程序是怎么解决我们日常生活中的场景的。
user_names = ['admin', 'aaron','peter','bob','Linus']
if user_names:
for name in user_names:
if name == 'admin':
print("Hello " + name + " ,would you like to see a status report?")
else:
print("Hello " + name + ",thanks you logging in again")
执行结果:
book@www.100ask.org:~/python/L7$ python admin.py
Hello admin ,would you like to see a status report?
Hello aaron,thanks you logging in again
Hello peter,thanks you logging in again
Hello bob,thanks you logging in again
Hello Linus,thanks you logging in again