2018-10-12 16:35:31 -04:00
|
|
|
|
---
|
|
|
|
|
title: If Elif Else Statements
|
2018-12-28 01:43:01 +08:00
|
|
|
|
localeTitle: If / Elif / Else语句
|
2018-10-12 16:35:31 -04:00
|
|
|
|
---
|
2018-12-28 01:43:01 +08:00
|
|
|
|
## If / Elif / Else语句
|
2018-10-12 16:35:31 -04:00
|
|
|
|
|
2018-12-28 01:43:01 +08:00
|
|
|
|
`if` / `elif` / `else`结构是控制程序流的常用方法,允许您根据某些数据的值执行特定的代码块。如果关键字`if`后面的条件表达式返回为`True` ,则代码块将执行: 请注意,在条件检查之前和之后不使用括号,如同其他语言一样。
|
2018-10-12 16:35:31 -04:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
if True:
|
|
|
|
|
print('If block will execute!')
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
x = 5
|
|
|
|
|
|
2018-12-28 01:43:01 +08:00
|
|
|
|
if x > 4:
|
2018-10-12 16:35:31 -04:00
|
|
|
|
print("The condition was true!") #this statement executes
|
|
|
|
|
```
|
|
|
|
|
|
2018-12-28 01:43:01 +08:00
|
|
|
|
您可以选择添加`else`执行条件为`False`的代码块:
|
2018-10-12 16:35:31 -04:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
if not True:
|
|
|
|
|
print('If statement will execute!')
|
2018-12-28 01:43:01 +08:00
|
|
|
|
else:
|
2018-10-12 16:35:31 -04:00
|
|
|
|
print('Else statement will execute!')
|
|
|
|
|
```
|
|
|
|
|
|
2018-12-28 01:43:01 +08:00
|
|
|
|
或者你也可以看看这个例子
|
2018-10-12 16:35:31 -04:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
y = 3
|
|
|
|
|
|
2018-12-28 01:43:01 +08:00
|
|
|
|
if y > 4:
|
2018-10-12 16:35:31 -04:00
|
|
|
|
print("I won't print!") #this statement does not execute
|
2018-12-28 01:43:01 +08:00
|
|
|
|
else:
|
2018-10-12 16:35:31 -04:00
|
|
|
|
print("The condition wasn't true!") #this statement executes
|
|
|
|
|
```
|
|
|
|
|
|
2018-12-28 01:43:01 +08:00
|
|
|
|
_请注意, `else`关键字后面没有条件 - 它捕获条件为`False`所有情况_
|
2018-10-12 16:35:31 -04:00
|
|
|
|
|
2018-12-28 01:43:01 +08:00
|
|
|
|
可以通过在初始`if`语句之后包含一个或多个`elif`来检查多个条件,但只执行一个条件:
|
2018-10-12 16:35:31 -04:00
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
z = 7
|
|
|
|
|
|
2018-12-28 01:43:01 +08:00
|
|
|
|
if z > 8:
|
2018-10-12 16:35:31 -04:00
|
|
|
|
print("I won't print!") #this statement does not execute
|
2018-12-28 01:43:01 +08:00
|
|
|
|
elif z > 5:
|
2018-10-12 16:35:31 -04:00
|
|
|
|
print("I will!") #this statement will execute
|
2018-12-28 01:43:01 +08:00
|
|
|
|
elif z > 6:
|
2018-10-12 16:35:31 -04:00
|
|
|
|
print("I also won't print!") #this statement does not execute
|
2018-12-28 01:43:01 +08:00
|
|
|
|
else:
|
2018-10-12 16:35:31 -04:00
|
|
|
|
print("Neither will I!") #this statement does not execute
|
|
|
|
|
```
|
|
|
|
|
|
2018-12-28 01:43:01 +08:00
|
|
|
|
_请注意,只有第一个计算为`True`条件才会执行。即使`z > 6`为`True` , `if/elif/else`块在第一个真实条件之后终止。这意味着只有在没有条件为`True`的情况下才会执行`else` 。_
|
2018-10-12 16:35:31 -04:00
|
|
|
|
|
|
|
|
|
我们还可以创建嵌套if用于决策。在此之前请参阅前面的href ='https://guide.freecodecamp.org/python/code-blocks-and-indentation'target ='\_ blank'rel ='nofollow'>缩进指南。
|
|
|
|
|
|
2018-12-28 01:43:01 +08:00
|
|
|
|
让我们举个例子来找一个偶数大于'10'的数字
|
2018-10-12 16:35:31 -04:00
|
|
|
|
```
|
|
|
|
|
python
|
2018-12-28 01:43:01 +08:00
|
|
|
|
x = 34
|
|
|
|
|
if x % 2 == 0: # this is how you create a comment and now, checking for even.
|
2018-10-12 16:35:31 -04:00
|
|
|
|
if x > 10:
|
|
|
|
|
print("This number is even and is greater than 10")
|
|
|
|
|
else:
|
|
|
|
|
print("This number is even, but not greater 10")
|
2018-12-28 01:43:01 +08:00
|
|
|
|
else:
|
2018-10-12 16:35:31 -04:00
|
|
|
|
print ("The number is not even. So point checking further.")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
这只是嵌套if的一个简单示例。请随时在线浏览更多内容。
|
|
|
|
|
|
|
|
|
|
虽然上面的示例很简单,但您可以使用[布尔比较](https://guide.freecodecamp.org/python/comparisons)和[布尔运算符](https://guide.freecodecamp.org/python/boolean-operations)创建复杂条件。
|
|
|
|
|
|
|
|
|
|
**_内联python if-else语句_**
|
|
|
|
|
|
2018-12-28 01:43:01 +08:00
|
|
|
|
我们也可以使用if-else语句内联python函数。以下示例应检查数字是否大于或等于50,如果是,则返回`True`:
|
2018-10-12 16:35:31 -04:00
|
|
|
|
```
|
|
|
|
|
python
|
2018-12-28 01:43:01 +08:00
|
|
|
|
x = 89
|
|
|
|
|
is_greater = True if x >= 50 else False
|
2018-10-12 16:35:31 -04:00
|
|
|
|
|
2018-12-28 01:43:01 +08:00
|
|
|
|
print(is_greater)
|
2018-10-12 16:35:31 -04:00
|
|
|
|
```
|
|
|
|
|
|
2018-12-28 01:43:01 +08:00
|
|
|
|
输出
|
2018-10-12 16:35:31 -04:00
|
|
|
|
```
|
|
|
|
|
>
|
2018-12-28 01:43:01 +08:00
|
|
|
|
True
|
|
|
|
|
>
|
2018-10-12 16:35:31 -04:00
|
|
|
|
|
2018-12-28 01:43:01 +08:00
|
|
|
|
```
|