2019-06-06 00:15:25 +08:00
|
|
|
|
## 分支结构
|
2018-03-02 00:09:06 +08:00
|
|
|
|
|
2019-07-10 00:10:36 +08:00
|
|
|
|
### 应用场景
|
2018-03-02 00:09:06 +08:00
|
|
|
|
|
2019-07-10 00:10:36 +08:00
|
|
|
|
迄今为止,我们写的Python代码都是一条一条语句顺序执行,这种代码结构通常称之为顺序结构。然而仅有顺序结构并不能解决所有的问题,比如我们设计一个游戏,游戏第一关的通关条件是玩家获得1000分,那么在完成本局游戏后,我们要根据玩家得到分数来决定究竟是进入第二关,还是告诉玩家“Game Over”,这里就会产生两个分支,而且这两个分支只有一个会被执行。类似的场景还有很多,我们将这种结构称之为“分支结构”或“选择结构”。给大家一分钟的时间,你应该可以想到至少5个以上这样的例子,赶紧试一试。
|
2018-03-02 00:09:06 +08:00
|
|
|
|
|
|
|
|
|
### if语句的使用
|
|
|
|
|
|
2019-07-10 00:10:36 +08:00
|
|
|
|
在Python中,要构造分支结构可以使用`if`、`elif`和`else`关键字。所谓**关键字**就是有特殊含义的单词,像`if`和`else`就是专门用于构造分支结构的关键字,很显然你不能够使用它作为变量名(事实上,用作其他的标识符也是不可以)。下面的例子中演示了如何构造一个分支结构。
|
2018-03-02 00:09:06 +08:00
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
"""
|
|
|
|
|
用户身份验证
|
|
|
|
|
|
|
|
|
|
Version: 0.1
|
|
|
|
|
Author: 骆昊
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
username = input('请输入用户名: ')
|
|
|
|
|
password = input('请输入口令: ')
|
|
|
|
|
# 如果希望输入口令时 终端中没有回显 可以使用getpass模块的getpass函数
|
|
|
|
|
# import getpass
|
|
|
|
|
# password = getpass.getpass('请输入口令: ')
|
|
|
|
|
if username == 'admin' and password == '123456':
|
2019-05-03 21:53:37 +08:00
|
|
|
|
print('身份验证成功!')
|
2018-03-02 00:09:06 +08:00
|
|
|
|
else:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
print('身份验证失败!')
|
2018-03-02 00:09:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
唯一需要说明的是和C/C++、Java等语言不同,Python中没有用花括号来构造代码块而是使用了缩进的方式来设置代码的层次结构,如果`if`条件成立的情况下需要执行多条语句,只要保持多条语句具有相同的缩进就可以了,换句话说连续的代码如果又保持了相同的缩进那么它们属于同一个代码块,相当于是一个执行的整体。
|
|
|
|
|
|
|
|
|
|
当然如果要构造出更多的分支,可以使用`if…elif…else…`结构,例如下面的分段函数求值。
|
|
|
|
|
|
2019-05-12 22:53:50 +08:00
|
|
|
|
![$$f(x)=\begin{cases} 3x-5&\text{(x>1)}\\x+2&\text{(-1}\leq\text{x}\leq\text{1)}\\5x+3&\text {(x<-1)}\end{cases}$$](./res/formula_1.png)
|
2018-03-02 00:09:06 +08:00
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
"""
|
|
|
|
|
分段函数求值
|
|
|
|
|
|
|
|
|
|
3x - 5 (x > 1)
|
|
|
|
|
f(x) = x + 2 (-1 <= x <= 1)
|
|
|
|
|
5x + 3 (x < -1)
|
|
|
|
|
|
|
|
|
|
Version: 0.1
|
|
|
|
|
Author: 骆昊
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
x = float(input('x = '))
|
|
|
|
|
if x > 1:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
y = 3 * x - 5
|
2018-03-02 00:09:06 +08:00
|
|
|
|
elif x >= -1:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
y = x + 2
|
2018-03-02 00:09:06 +08:00
|
|
|
|
else:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
y = 5 * x + 3
|
2018-03-02 00:09:06 +08:00
|
|
|
|
print('f(%.2f) = %.2f' % (x, y))
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
当然根据实际开发的需要,分支结构是可以嵌套的,例如判断是否通关以后还要根据你获得的宝物或者道具的数量对你的表现给出等级(比如点亮两颗或三颗星星),那么我们就需要在`if`的内部构造出一个新的分支结构,同理`elif`和`else`中也可以再构造新的分支,我们称之为嵌套的分支结构,也就是说上面的代码也可以写成下面的样子。
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
"""
|
|
|
|
|
分段函数求值
|
|
|
|
|
3x - 5 (x > 1)
|
|
|
|
|
f(x) = x + 2 (-1 <= x <= 1)
|
|
|
|
|
5x + 3 (x < -1)
|
|
|
|
|
|
|
|
|
|
Version: 0.1
|
|
|
|
|
Author: 骆昊
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
x = float(input('x = '))
|
|
|
|
|
if x > 1:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
y = 3 * x - 5
|
2018-03-02 00:09:06 +08:00
|
|
|
|
else:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
if x >= -1:
|
|
|
|
|
y = x + 2
|
|
|
|
|
else:
|
|
|
|
|
y = 5 * x + 3
|
2018-03-02 00:09:06 +08:00
|
|
|
|
print('f(%.2f) = %.2f' % (x, y))
|
|
|
|
|
```
|
|
|
|
|
|
2019-07-10 00:10:36 +08:00
|
|
|
|
> **说明:** 大家可以自己感受一下这两种写法到底是哪一种更好。在之前我们提到的Python之禅中有这么一句话“Flat is better than nested.”,之所以提倡代码“扁平化”是因为嵌套结构的嵌套层次多了之后会严重的影响代码的可读性,所以能使用扁平化的结构时就不要使用嵌套。
|
2018-03-02 00:09:06 +08:00
|
|
|
|
|
|
|
|
|
### 练习
|
|
|
|
|
|
|
|
|
|
#### 练习1:英制单位与公制单位互换
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
"""
|
|
|
|
|
英制单位英寸和公制单位厘米互换
|
|
|
|
|
|
|
|
|
|
Version: 0.1
|
|
|
|
|
Author: 骆昊
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
value = float(input('请输入长度: '))
|
|
|
|
|
unit = input('请输入单位: ')
|
|
|
|
|
if unit == 'in' or unit == '英寸':
|
2019-05-03 21:53:37 +08:00
|
|
|
|
print('%f英寸 = %f厘米' % (value, value * 2.54))
|
2018-03-02 00:09:06 +08:00
|
|
|
|
elif unit == 'cm' or unit == '厘米':
|
2019-05-03 21:53:37 +08:00
|
|
|
|
print('%f厘米 = %f英寸' % (value, value / 2.54))
|
2018-03-02 00:09:06 +08:00
|
|
|
|
else:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
print('请输入有效的单位')
|
2018-03-02 00:09:06 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 练习2:掷骰子决定做什么
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
"""
|
|
|
|
|
掷骰子决定做什么事情
|
|
|
|
|
|
|
|
|
|
Version: 0.1
|
|
|
|
|
Author: 骆昊
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from random import randint
|
|
|
|
|
|
|
|
|
|
face = randint(1, 6)
|
|
|
|
|
if face == 1:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
result = '唱首歌'
|
2018-03-02 00:09:06 +08:00
|
|
|
|
elif face == 2:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
result = '跳个舞'
|
2018-03-02 00:09:06 +08:00
|
|
|
|
elif face == 3:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
result = '学狗叫'
|
2018-03-02 00:09:06 +08:00
|
|
|
|
elif face == 4:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
result = '做俯卧撑'
|
2018-03-02 00:09:06 +08:00
|
|
|
|
elif face == 5:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
result = '念绕口令'
|
2018-03-02 00:09:06 +08:00
|
|
|
|
else:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
result = '讲冷笑话'
|
2018-03-02 00:09:06 +08:00
|
|
|
|
print(result)
|
|
|
|
|
```
|
2019-05-09 17:12:55 +08:00
|
|
|
|
> **说明:** 上面的代码中使用了random模块的randint函数生成指定范围的随机数来模拟掷骰子。
|
2018-03-02 00:09:06 +08:00
|
|
|
|
|
|
|
|
|
#### 练习3:百分制成绩转等级制
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
"""
|
|
|
|
|
百分制成绩转等级制成绩
|
|
|
|
|
90分以上 --> A
|
2019-09-15 00:38:27 +08:00
|
|
|
|
80分~89分 --> B
|
|
|
|
|
70分~79分 --> C
|
|
|
|
|
60分~69分 --> D
|
2018-03-02 00:09:06 +08:00
|
|
|
|
60分以下 --> E
|
|
|
|
|
|
|
|
|
|
Version: 0.1
|
|
|
|
|
Author: 骆昊
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
score = float(input('请输入成绩: '))
|
|
|
|
|
if score >= 90:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
grade = 'A'
|
2018-03-02 00:09:06 +08:00
|
|
|
|
elif score >= 80:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
grade = 'B'
|
2018-03-02 00:09:06 +08:00
|
|
|
|
elif score >= 70:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
grade = 'C'
|
2018-03-02 00:09:06 +08:00
|
|
|
|
elif score >= 60:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
grade = 'D'
|
2018-03-02 00:09:06 +08:00
|
|
|
|
else:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
grade = 'E'
|
2018-03-02 00:09:06 +08:00
|
|
|
|
print('对应的等级是:', grade)
|
|
|
|
|
```
|
|
|
|
|
#### 练习4:输入三条边长如果能构成三角形就计算周长和面积
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
"""
|
|
|
|
|
判断输入的边长能否构成三角形
|
|
|
|
|
如果能则计算出三角形的周长和面积
|
|
|
|
|
|
|
|
|
|
Version: 0.1
|
|
|
|
|
Author: 骆昊
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import math
|
|
|
|
|
|
|
|
|
|
a = float(input('a = '))
|
|
|
|
|
b = float(input('b = '))
|
|
|
|
|
c = float(input('c = '))
|
|
|
|
|
if a + b > c and a + c > b and b + c > a:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
print('周长: %f' % (a + b + c))
|
|
|
|
|
p = (a + b + c) / 2
|
|
|
|
|
area = math.sqrt(p * (p - a) * (p - b) * (p - c))
|
|
|
|
|
print('面积: %f' % (area))
|
2018-03-02 00:09:06 +08:00
|
|
|
|
else:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
print('不能构成三角形')
|
2018-03-02 00:09:06 +08:00
|
|
|
|
```
|
2019-05-09 17:12:55 +08:00
|
|
|
|
> **说明:** 上面的代码中使用了`math`模块的`sqrt`函数来计算平方根。用边长计算三角形面积的公式叫做[海伦公式](https://zh.wikipedia.org/zh-hans/海伦公式)。
|
2018-03-02 00:09:06 +08:00
|
|
|
|
|
2018-12-22 16:26:07 +08:00
|
|
|
|
#### 练习5:个人所得税计算器。
|
2018-03-02 00:09:06 +08:00
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
"""
|
2019-09-15 00:38:27 +08:00
|
|
|
|
输入月收入和五险一金计算个人所得税(2019年以前的算法)
|
2018-03-02 00:09:06 +08:00
|
|
|
|
|
|
|
|
|
Version: 0.1
|
|
|
|
|
Author: 骆昊
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
salary = float(input('本月收入: '))
|
|
|
|
|
insurance = float(input('五险一金: '))
|
|
|
|
|
diff = salary - insurance - 3500
|
|
|
|
|
if diff <= 0:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
rate = 0
|
|
|
|
|
deduction = 0
|
2018-03-02 00:09:06 +08:00
|
|
|
|
elif diff < 1500:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
rate = 0.03
|
|
|
|
|
deduction = 0
|
2018-03-02 00:09:06 +08:00
|
|
|
|
elif diff < 4500:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
rate = 0.1
|
|
|
|
|
deduction = 105
|
2018-03-02 00:09:06 +08:00
|
|
|
|
elif diff < 9000:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
rate = 0.2
|
|
|
|
|
deduction = 555
|
2018-03-02 00:09:06 +08:00
|
|
|
|
elif diff < 35000:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
rate = 0.25
|
|
|
|
|
deduction = 1005
|
2018-03-02 00:09:06 +08:00
|
|
|
|
elif diff < 55000:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
rate = 0.3
|
|
|
|
|
deduction = 2755
|
2018-03-02 00:09:06 +08:00
|
|
|
|
elif diff < 80000:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
rate = 0.35
|
|
|
|
|
deduction = 5505
|
2018-03-02 00:09:06 +08:00
|
|
|
|
else:
|
2019-05-03 21:53:37 +08:00
|
|
|
|
rate = 0.45
|
|
|
|
|
deduction = 13505
|
2018-03-02 00:09:06 +08:00
|
|
|
|
tax = abs(diff * rate - deduction)
|
|
|
|
|
print('个人所得税: ¥%.2f元' % tax)
|
|
|
|
|
print('实际到手收入: ¥%.2f元' % (diff + 3500 - tax))
|
|
|
|
|
```
|
2019-05-09 17:12:55 +08:00
|
|
|
|
>**说明:** 上面的代码中使用了Python内置的`abs()`函数取绝对值来处理`-0`的问题。
|