如果学生成绩为浮点数如95.5,我该如何改进
while 1:print("请输入你想验证的分数")
temp=input()
if temp.isdigit():
score=float(temp)
if 0.0 <= score <=60:
print("不及格")
elif 60 <score<80:
print("及格")
elif 80 <= score<90:
print("良好")
elif 90 <= score <=100:
print("优秀")
else:
print("请输入正确的分数")
else:
print("输入有误,请输入正确已数字表现的分数")
while 1:
print("请输入你想验证的分数")
temp=input()
try:
score=float(temp)
if 0.0 <= score <=60:
print("不及格")
elif 60 <score<80:
print("及格")
elif 80 <= score<90:
print("良好")
elif 90 <= score <=100:
print("优秀")
else:
print("请输入正确的分数")
except:
print("输入有误,请输入正确已数字表现的分数")
while True:
try:
score = float(input("请输入你想验证的分数:"))
except:
print("输入有误,请输入正确已数字表现的分数")
continue
print("优秀") if score >= 90 else\
print("良好") if score >= 80 else\
print("及格") if score > 60 else\
print("不及格")请输入你想验证的分数:95.5
优秀
请输入你想验证的分数:53
不及格
请输入你想验证的分数:75
及格
请输入你想验证的分数:qq
输入有误,请输入正确已数字表现的分数 逃兵 发表于 2021-10-28 15:24
谢谢大佬,自己又去查询了try,except的用法。
为什么我在输入小数的时候会报错呢?s.isdigit()不是判断是不是数字吗?是不是因为有个小数点的原因呢? Python-LX 发表于 2021-10-28 15:50
谢谢大佬,自己又去查询了try,except的用法。
为什么我在输入小数的时候会报错呢?s.isdigit()不是判断 ...
str.isdigit()
只包含整数才为True
有小数点就是False
页:
[1]