215330 发表于 2017-12-15 17:32:35

修改代码

我想将用户输入的分数限制在(1,100)的范围内,其余的输如均为无效数据应该什么修改代码呀?

°蓝鲤歌蓝 发表于 2017-12-15 17:55:08

score = int(input("请输入您的分数:"))
while score in range(1,101):
    if:
      print
    if:
      print
    if:
      print
    else:
      print

215330 发表于 2017-12-15 17:56:36

°蓝鲤歌蓝 发表于 2017-12-15 17:55


谢谢,我想到了加范围限制就是不知道怎么加,加在那个位置。要学的还很多啊啊啊啊

°蓝鲤歌蓝 发表于 2017-12-15 17:57:47

if score>100 or score<1:
   print("数据无效")

215330 发表于 2017-12-15 18:11:02

本帖最后由 215330 于 2017-12-15 18:12 编辑

°蓝鲤歌蓝 发表于 2017-12-15 17:55

666

ba21 发表于 2017-12-15 18:16:55

if 0<=score<60:
    print
elif 60<=score<80:
    print
elif 80<=score<90:
    print
elif 90<=score<=100:
    print
else:
    print("输入无效")

215330 发表于 2017-12-15 18:19:25

ba21 发表于 2017-12-15 18:16


谢谢老铁!

215330 发表于 2017-12-15 18:20:25

本帖最后由 215330 于 2017-12-15 18:28 编辑

score = int (input("请输入您的分数:"))

if score>100 or score<1:
    print("数据无效")
if 0<=score<60:
    print("D")
if 60<=score<80:
    print("C")
if 80<=score<90:
      print("B")
if 90<=score<=100:
    print("A")

最后的代码是这样的,满足了我想要的了。谢谢楼上俩老铁

老八秘制 发表于 2020-3-28 10:28:44


try:
    score = int(input('请输入您的分数'))
except (ValueError, EOFError, KeyboardInterrupt):
    print('输入错误!')
if 60 <= score < 80:
    print("C")
elif 80 <= score < 90:
    print("B")
elif 90 <= score < 100:
    print("A")
elif 60 > score:
    print("D")
else:
   print('输入错误!')
页: [1]
查看完整版本: 修改代码