Python-LX 发表于 2021-10-28 15:21:29

如果学生成绩为浮点数如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("输入有误,请输入正确已数字表现的分数")

逃兵 发表于 2021-10-28 15:24:17


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("输入有误,请输入正确已数字表现的分数")

傻眼貓咪 发表于 2021-10-28 15:38:28

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
输入有误,请输入正确已数字表现的分数

Python-LX 发表于 2021-10-28 15:50:17

逃兵 发表于 2021-10-28 15:24


谢谢大佬,自己又去查询了try,except的用法。
为什么我在输入小数的时候会报错呢?s.isdigit()不是判断是不是数字吗?是不是因为有个小数点的原因呢?

逃兵 发表于 2021-10-28 16:49:50

Python-LX 发表于 2021-10-28 15:50
谢谢大佬,自己又去查询了try,except的用法。
为什么我在输入小数的时候会报错呢?s.isdigit()不是判断 ...

str.isdigit()
只包含整数才为True
有小数点就是False
页: [1]
查看完整版本: 如果学生成绩为浮点数如95.5,我该如何改进