1.单分支if
例子:
if 3>2:
print(1)
2.if……else
#注意Python中的缩进 if 3>5: print(1) else: print(2)
3.if……else if……else
score=input("输入") score=int(score) if 0 <= score <= 60: print("D") elif 60 <= score <= 70: print("B") else: print("哇哦") #缩代码 level=('D' if 0 <= score <60 else 'C' if 60 <= score <80 else 'D' if 80 <= score <=100 else "请输入值") print(level)
4.分支嵌套
#分支结构的嵌套 isriot=True age=23 if age>20: if isriot==False: print("你") else: if isriot==True: print("zai")