《老鸟python 系列》视频上线了,全网稀缺资源,涵盖python人工智能教程,爬虫教程,web教程,数据分析教程以及界面库和服务器教程,以及各个方向的主流实用项目,手把手带你从零开始进阶高手之路!点击 链接 查看详情




条件判断

阅读:227569067    分享到

我们现实世界中存在着条件判断,比如分数大于等于 60 分就是及格否则就是不及格,有喉结就是男生否则就是女生, 还有如果长得帅车就会爆胎......。计算机程序所做条件判断,和在现实世界中我们所做的条件判断是一样的。

Python 条件语句是通过一条或多条语句的执行结果(真或假)来决定执行的代码块,各种数据类型的假值有: 整数 0,浮点数 0.0, 字符串 "", 动态数组 [], 元组 (), 集合 set([]), 字典 {} 和自定义类型空对象,对于所有类型的非假值即为真值。

if 语句

if 判断语句为真则执行代码块,否则不执行。

score = 60
if score >= 60:
    print("及格")

重要的事情再说一遍:if 判断会把参与运算的各种类型的对象值看作真假,对于各种数据类型的假值有: 整数 0, 浮点数 0.0, 字符串 "", 动态数组 [], 元组 (), 集合 set([]), 字典 {} 和自定义类型空对象, 对于所有类型的非假值即为真值。

a = -1
if a:
    print("真值")

if 判断的语句可以是任何对象或者表达式。

a = -0.1
b = "hello"
c = [1, 2]
d = (1, 2)
e = {'b': b, 'c': c}

if a and b and c and d:
    print(a)
    print(b)
    print(c)
    print(d)
    print(e)

else 语句

也可以给 if 语句添加一个 else 语句,意思就是不满足 if 语句的条件,就执行 else 下面的语句。

score = 60
if score >= 60:
    print("及格")
else:
    print("不及格")

注意 else 语句必须结合 if 语句使用。

elif 语句

elif 是 else if 的缩写,我们可以把 elif 看作是在 else 成立的基础上再进行 if 判断, 所以 elif 判断的结果是 else 的子集,我们可以使用多个 elif 进行筛选 else 的结果,注意:elif 语句后面要有判断语句,而else后面没有判断语句。

name = "比尔盖茨"
if name == "扎克伯格":
    print("facebook 老板")
elif name == "乔布斯":
    print("苹果老板")
elif name == "贾跃亭":
    print("骗子老板")
elif name == "比尔盖茨":
    print("微软老板")
else:
    print("不知道是谁的老板")

从第一个判断语句开始,如果这个语句返回的结果是真值,就忽略掉剩下的 elif 和 else。

score = 88
if score >= 60:   # 只有这句代码起作用
    print("及格")
elif score >= 70:
    print("中等")
elif score >= 80:
    print("良好")
elif score >= 90:
    print("优秀")

本节重要知识点

深刻理解 if 判断的语句是真假而不是 True 或 False。

弄明白 if 判断的一些陷阱。

作业

著名公司(某德地图)根据学生分数做相应的输出,当分数在 [0, 60) 之间输出不及格, [60, 70) 之间输出及格, [70, 80) 之间输出良好, [80, 100] 之间输出优秀,其它情况输出分数不合理,注意:确保程序健壮性(比如对用户输入非数字类型做异常处理)。 小说明:[low, high) 是左闭右开,表达的意思是大于等于 low 小于 high;[low, high] 是左闭右闭表达的意思是大于等于 low 小于等于 high。

score = input("输入分数:")

# 请完成代码

如果以上内容对您有帮助,请老板用微信扫一下赞赏码,赞赏后加微信号 birdpython 领取免费视频。


登录后评论

user_image
zxzx19900109
2024年2月21日 16:54 回复
while True:
    score = input("输入分数:")
    try:
        score = int(score.strip())
        if not (0 <= score <= 100):
            raise ValueError('分数必须0-100之间')
        break
    except ValueError as e:
        print('输入有误,请重试')

if 80 <= score <= 100:
    print('优秀')
elif 70 <= score < 80:
    print('良好')
elif 60 <= score < 70:
    print('合格')
else:
    print('不合格')

user_image
Seven
2021年11月11日 20:27 回复

作业

score = input('Please enter your score:') score = int(score) if score <= 60: print('Flunk') elif score >=60 and score <= 70: print('pass') elif score >= 70 and score <= 80: print('Good') elif score >= 80: print('Great')


user_image
飒蓝
2020年8月20日 12:40 回复
score = int(input("输入分数:"))

if 0<=score < 60:
    print("不及格")
elif score<70:
    print("及格")
elif score<80:
    print("良好")
elif score<=100:
    print("优秀")

user_image
高高
2020年5月31日 13:00 回复
score = int(input('输入分数:'))
if score >= 90:
    print('优秀')
elif score >=80:
    print('良好')
elif score >= 70:
    print('中等')
elif score >=60:
    print('及格')
elif score >= 0:
    print('不及格')
else:
    print('输入错误!')

user_image
袁来如痴
2020年5月18日 03:48 回复
while True:
    ten=input("输入分数:")
    try:
        score=eval(ten)
        if type(score)==int:
            break
    except:
            print("请输入数字")
            pass
if score > 100 or score <0:
    print("分数不合理")
elif score >= 80:
    print("优秀")
elif score >= 70:
    print("良好")
elif score >= 60:
    print("及格")
else: print("不及格")

user_image
康智冬
2020年5月5日 19:50 回复
score = input("输入分数:")
score =int(score)
if score >= 80 and score <100:
    print("优秀")
elif score >= 70 and score < 80:
    print("良好")
elif score >= 60 and score < 70:
    print("及格")
elif score >=0 and score <=60:
    print("不及格")                           else :
    print("输入出错!")

user_image
Sarakeal
2020年2月26日 05:32 回复
score=int(input("请输入分数:"))
if score <= 60:
    print("不及格")
elif score <= 70:
    print("及格")
elif score <= 80:
    print("良好")
elif score <= 100:
    print("优秀")
elif score < 0 or score > 100 :
    print("数据不合理")

user_image
Zhaoyang
2020年2月21日 04:43 回复
score = input("输入分数:")
score =int(score)
if score >= 80 and score <100:
    print("优秀")
elif score >= 70 and score < 80:
    print("良好")
elif score >= 60 and score < 70:
    print("及格")
elif score < 60:
    print("不及格")

user_image
Liao
2019年11月1日 22:46 回复
score = int(input("请输入分数:"))
if score >= 0:
    if score < 60:
        print("不及格")
    elif score < 70:
        print("及格")
    elif score < 80:
        print("良好")
    elif score <= 100:
        print("优秀")
else:
    print("分数不合理")

user_image
路人甲
2019年7月1日 22:44 回复
try:
    score = input("输入分数:")
    s = int(score)
    if s < 0:
        print("不能小于0")
    elif s < 60:
        print("不及格")
    elif s < 70:
        print("及格")
    elif s < 80:
        print("良好")
    elif s <= 100:
        print("优秀")
    else:
        print("数值太大")

except ValueError:
    print("请输入数字")

user_image
王祖贤
2019年6月21日 06:17 回复
score = input("请输入分数")
score = int(score)


if 0 < score < 60:
    print("不及格")
elif score < 70:
    print("及格")
elif score < 80:
    print("良好")
else:
    print("优秀")

user_image
陈诺
2018年8月24日 07:24 回复

【求助!】为什么报错语句无效invalid syntax"严重肥胖"

BMI = weight / (height**2)

if BMI>32:

    print('%1f','严重肥胖',%BMI)

elif BMI>=28:

    print('%1f','肥胖',%BMI)

elif BMI>=25:

    print('%1f','过重',%BMI)

elif BMI>=18.5:

    print('%1f','正常',%BMI)

else BMI<18.5:

    print('%1f','过轻',%BMI)

user_image
想换个ID
2019年3月20日 18:25

print('%.1f, 严重肥胖' % BMI)