Python决策结构与布尔逻辑全解析
1. 嵌套块与条件测试
在编程中,经常会遇到需要测试一系列条件,然后根据哪个条件为真来执行相应操作的情况。一种实现方式是使用嵌套决策结构,即在一个决策结构中嵌套多个其他决策结构。
例如,有一个文学课老师苏亚雷斯博士,他使用10分制评分标准来评定考试成绩:
| 测试分数 | 等级 |
| ---- | ---- |
| 90及以上 | A |
| 80 - 89 | B |
| 70 - 79 | C |
| 60 - 69 | D |
| 低于60 | F |
以下是实现根据输入的测试分数显示对应等级的Python代码:
# This program gets a numeric test score from the
# user and displays the corresponding letter grade.
# Named constants to represent the grade thresholds
A_score = 90
B_score = 80
C_score = 70
D_score = 60
# Get a test score from the user.
score = int(input('Enter your test score: '))
# Determine the grade.
if score >= A_score:
print('Your grade is A.')
else:
if score >= B_
超级会员免费看
订阅专栏 解锁全文
2018

被折叠的 条评论
为什么被折叠?



