单向分支 双向分支 多向分支 巢状分支
- 写分支结构时,要严格遵守python语法规则
- 判断区间不能为空
- 相同代码块的代码保持相同的缩进
# 例:让用户输入一个年份,判断该年的生肖 # 鼠、牛、虎、兔、龙、蛇、马、羊、猴、鸡、狗、猪 zodiac = ['猴','鸡','狗','猪','鼠','牛','虎','兔','龙','蛇','马','羊'] year = input('请输入年份:') Y = int(year) % 12 print(f'{year}年的生肖为:'+ zodiac[Y])
W = 'world of warcraft' D = 'DOTA2' C = 'CS' G = 'GTA' S = 'dark souls' M = 'monster hunter' t = 90
if t > 0 and t <= 120 : print(W) world of warcraft
if t > 60 : print(D) else : print(W) DOTA2
if t > 0 and t <= 30 : print(W) elif t > 30 and t <= 60 : print(D) elif t > 60 and t <= 90 : print(C) elif t > 90 and t <= 120 : print(G) else : print(S) CS
study = 'finish' work = 'finish' fitness = 2 Time = 30 if study == 'finish' : if work == 'finish' : if fitness >= 2 : if Time > 20 : print(W) else : print('fitness') else : print('work') else : print('study') world of warcraft