鱼C论坛

 找回密码
 立即注册
查看: 1057|回复: 5

[已解决]请大神帮忙看一下这个代码为什么运行不出结果,也没报错

[复制链接]
发表于 2019-7-28 15:40:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
score=input("请输入一个分数")
while score == "":
    print("必须输入一个分数!")
    score=input("请重新输入分数")
while score != "":
    score=int(score)
    if score>100 or score<0:
        print("输入错误!")
        score=input("请重新输入分数")
if score != "":
    score =int(score)
    if 100 >= score >= 90:
        print('A')
    elif 90 >score >= 80:
        print('B')
    elif 80 >score >= 60:
        print('C')
    elif 0 <=score < 60:
        print('D')
print("查询结束")


上面二个while运行都没问题,下面的查询单独运行也能出结果,但放在一起的时候,不输数字和输入错误数字都能正常运行,反正是下面的出问题,求帮忙看一下。
最佳答案
2019-7-28 15:47:55
本帖最后由 zltzlt 于 2019-7-28 16:05 编辑
  1. score = input("请输入一个分数")
  2. while not score.isdigit():
  3.     print("必须输入一个分数!")
  4.     score = input("请重新输入分数")
  5. while not 0 <= int(score) <= 100:
  6.     print("必须输入一个分数!")
  7.     score = input("请重新输入分数")
  8. score = int(score)
  9. if 100 >= score >= 90:
  10.     print('A')
  11. elif 90 > score >= 80:
  12.     print('B')
  13. elif 80 > score >= 60:
  14.     print('C')
  15. elif 0 <= score < 60:
  16.     print('D')
  17. print("查询结束")
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-7-28 15:47:55 | 显示全部楼层    本楼为最佳答案   
本帖最后由 zltzlt 于 2019-7-28 16:05 编辑
  1. score = input("请输入一个分数")
  2. while not score.isdigit():
  3.     print("必须输入一个分数!")
  4.     score = input("请重新输入分数")
  5. while not 0 <= int(score) <= 100:
  6.     print("必须输入一个分数!")
  7.     score = input("请重新输入分数")
  8. score = int(score)
  9. if 100 >= score >= 90:
  10.     print('A')
  11. elif 90 > score >= 80:
  12.     print('B')
  13. elif 80 > score >= 60:
  14.     print('C')
  15. elif 0 <= score < 60:
  16.     print('D')
  17. print("查询结束")
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-7-28 15:55:32 | 显示全部楼层

请问我现在还没学到isdigit(),难道就不能用我那二个while来实现吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-28 16:02:45 | 显示全部楼层
zlmxl1987 发表于 2019-7-28 15:55
请问我现在还没学到isdigit(),难道就不能用我那二个while来实现吗

不可以,因为你那种方法输入字母会报错,输入0 ~ 100的数字会一直询问,输入其他的数字会没反应,我测试过了你的代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-7-28 16:08:25 | 显示全部楼层
zltzlt 发表于 2019-7-28 16:02
不可以,因为你那种方法输入字母会报错,输入0 ~ 100的数字会一直询问,输入其他的数字会没反应,我测试 ...

是的,我试过同时执行上面二个循环代码是没问题的,单独执行下面的代码也没问题,但放在一起执行数字就没反应,一直没想通是怎么回事
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-7-28 16:14:49 | 显示全部楼层
zltzlt 发表于 2019-7-28 16:02
不可以,因为你那种方法输入字母会报错,输入0 ~ 100的数字会一直询问,输入其他的数字会没反应,我测试 ...

谢谢了,我找到原因了,加个break就可以了
score=input("请输入一个分数")
while score=="":
    print("必须输入一个分数!")
    score=input("请重新输入分数")
while score !="":
    score=int(score)
    if score>100 or score<0:
        print("输入错误!")
        score=input("请重新输入分数")
    else:
        break
if score !="":
    score=int(score)
    if 100>=score>=90:
        print('A')
    elif 90>score>=80:
        print('B')
    elif 80>score>=60:
        print('C')
    elif 0<=score<60:
        print('D')
print("查询结束")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-21 19:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表