Python的if条件语句(常用)

基本形式:

if 判断条件:
执行语句
else
执行语句:

当判断条件为多个值时,采取以下形式:

if 判断条件1:
执行语句1
elif判断条件2:
执行语句2
elif 判断条件3:
执行语句3
……
else:
执行语句4

举例:

学生成绩在90-100是优秀
学生成绩在80-89是良好
学生成绩在60-79是合格
在60分以下为不合格
代码:

#coding=utf-8

score = 99

if score>=90 and score <=100:
    print("优秀")
elif score>=80 and score <=89:
    print("良好")
elif score>=60 and score <=79:
    print("合格")
elif score<=60 and score >=0:
    print("不及格")
else:
    print("分数不合法")

运行结果如下
在这里插入图片描述

原创文章 35 获赞 100 访问量 4287

猜你喜欢

转载自blog.csdn.net/hanhanwanghaha/article/details/105749690