《Python编程:从入门到实践(第二版)》自学笔记(六)

if语句结构

对于if语句结构,有以下几种情况,不过不管是哪种结构,在使用的过程中都应当注意条件测试和执行程序的格式,尤其是缩进

首先最简单的就是,一个条件测试,一个操作,不过个人喜欢把操作称为执行程序

1
2
3
##仅为示范
if conditional_test:
do something
1
2
3
4
age = 19
if age >= 18:
print("You are old enough to vote!")
输出:You are old enough to vote!

此处缩进的作用与在for循环中相同,如果测试通过了,将执行if语句后面所有缩进的代码行,否则将忽略它们

其次if语句结构,在英语中经常接触到,就是if……else……,此结构一般运用在条件测试通过时执行一个操作,在没有通过时执行另一个操作

1
2
3
4
5
6
7
8
9
10
age = 17
if age >= 18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")
else:
print("Sorry, you are too young to vote.")
print("Please register to vote as soon as you turn 18!")
输出:
Sorry, you are too young to vote.
Please register to vote as soon as you turn 18!

另外,在常见的if语句结构中还会看到:if……elif……else,这种结构。它的意思是:依次检查每个条件测试,直到遇到通过了的条件测试。测试通过后,Python将执行紧跟在它后面的代码,并跳过余下的测试

1
2
3
4
5
6
7
8
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.")
输出:Your admission cost is $25.

为使代码更加简单轻量化,对原代码进行优化

1
2
3
4
5
6
7
8
9
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
else:
price = 40
print(f"Your admission cost is ${price}.")
输出:Your admission cost is $25.

这里需要插一句嘴的是:elif代码块并不是只能有一个,它是可以有多个穿插在if和else中间的,但带来的缺点就是:臃肿

1
2
3
4
5
6
7
8
9
10
11
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}.")
输出:Your admission cost is $25.

与此同时,既然elif代码可以有多个,那么else代码块也可以省略。在Python中并不要求if-elif结构后面必须有else代码块,在有些情况下,else代码块很有用,而在其他一些情况下,使用一条elif语句来处理特定的情形更清晰

1
2
3
4
5
6
7
8
9
10
11
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}.")
输出:Your admission cost is $25.

总之,如果只想执行一个代码块,就使用if……elif……else结构;如果要 执行多个代码块,就使用一系列独立的if语句

当遇到需要if语句去处理列表时,也可以结合之前的遍历这一块的内容,也就是使用for去进行处理

1
2
3
4
5
6
7
8
9
10
11
12
13
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(f"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!

但是也有可能会遇到一种个特殊的情况,就是:列表为空这种情况

1
2
3
4
5
6
7
8
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?

同样的,还有一种情况就是,对多个列表进行处理时,又将如何去进行处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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}.")

print("\nFinished making your pizza!")
输出:
Adding mushrooms.
Sorry, we don't have french fries.
Adding extra cheese.

Finished making your pizza!

以上所有使用到for进行遍历和if语句结构的,都要仔细检查最后面的冒号,冒号,冒号!!!

打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2019-2024 Carrol Chen
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信