Python编程中if语句用于控制程序的执行,基本形式为:
if 判断条件:
执行语句…
else 为可选语句,当需要在条件下不成立时执行内容则可以执行相关语句
案例1:根据分数来判断学生成绩是否为优秀,80分以上为优秀,评级为A
score=80
if score>=80:
print("Score is A")
else:
print("Score is not A")
注意:print语句要注意缩进,不要Tab和空格混用,否则回变编译报错
案例2:成绩80分以上为评级A,60-79分为B,60分以下为C
score=58
if score>=80:
print("Score is A")
elif score>=60:
print("Score is B")
else:
print("Score is C")
Tips:elif是else if的缩写,完全可以有多个elif