分支结构_多分支结构

多分支结构
一般针对的是一个连续区间段的不停的处理操作
语法结构:
if 条件表达式1:
条件执行体1
elif 条件表达式2:
条件执行体2
elif 条件表达式N:
条件执行体N
else:
条件执行体N+1

注意这边的选择与C中一样至多只会执行其中的一个语句执行体
python中允许多分支语句没有else的存在
example:
a = int(input('Please input a number'))
if a==2:
print('hello world')
elif a:
print('Present')

即多分支结构中的else可以省略,但是双分支结构中的else不可以省略,否则就会退化成单分支结构
example:#表示对成绩的分类的程序
score = int(input('Please input a score : '))

if score <= 100 and score >= 90:
print('A')
elif score >= 80 and score < 90:
print('B')
elif score >= 70 and score < 80:
print('C')
elif score >= 60 and score < 70:
print('D')
elif score >= 0 and score < 60:
print('E')
else:
print('Your input is illegal')

注意python非常符合逻辑的地方出现了,其他地方的语言一般不支持
score<=100 and score>=90
90<=score<=100
这两种在python中是一样的

posted @   banyanrong  阅读(934)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示