Python
问题在哪??while True:
temp = input('请输入一个整数')
while not temp.isdigit:
temp = input("输入错误,请重新输入。")
secret = int(temp)
while secret > 100 or secret < 0:
temp = input("请输入0到100之前的数字。")
secret = int(temp)
if secret>80 and secret <= 100:
print("A")
elif secret<=80 and secret>60:
print("B")
elif secret>40 and secret<=60:
print("C")
else:
print("D")
为啥标题就六个字母???
1. 缩进错了
2. isdigit() 漏了括号
帮你改好了:
while True:
temp = input('请输入一个整数')
while not temp.isdigit(): # 更改
temp = input("输入错误,请重新输入。")
secret = int(temp)
while secret > 100 or secret < 0:
temp = input("请输入0到100之前的数字。") # 更改
secret = int(temp) # 更改
if secret > 80 and secret <= 100:
print("A")
elif secret <= 80 and secret > 60:
print("B")
elif secret > 40 and secret <= 60:
print("C")
else:
print("D") while True:
temp = input('请输入一个整数')
while not temp.isdigit():
temp = input("输入错误,请重新输入。")
secret = int(temp)
while secret > 100 or secret < 0:
temp = input("请输入0到100之前的数字。")
secret = int(temp)
if secret > 80 and secret <= 100:
print("A")
elif secret <= 80 and secret > 60:
print("B")
elif secret > 40 and secret <= 60:
print("C")
else:
print("D") 帮你修改了,问题写在注释里了。你的程序和三楼的程序,在遇到输入范围不对的情况下,重新输入后,没有进行输入格式判断,可能会出错哟。
while True:
temp = input('请输入一个整数')
while True: #将判断输入格式问题和输入范围问题放在同一个循环下,这样输入范围不对的情况下再次输入还会判断格式是否正确
if not temp.isdigit(): # 这里isdigit是一个函数,调用时,后面需要加上括号
temp = input("输入错误,请重新输入。")#若因格式不对而进入这个if语句块,直接进行下一次输入
continue #若因格式不对而进入这个if语句块,就不再进行int转换也不再判断数字范围,直接进行下一次循环
secret = int(temp) #能执行这一句说明if语句通过了,输入格式是正确的,yongint将之转为数字
if secret > 100 or secret < 0:#if语句判断输入范围,范围不正确,重新输入,然后进行下一次循环,重新判断输入格式
temp = input("请输入0到100之前的数字。") # 之前这里缩进不对,而且这里重新输入之后没有判断格式是否是数字,就直接用int转化,运行时可能会弹出异常
else: #若输入数字的范围正确就跳出这个循环
break
if secret > 80 and secret <= 100:
print("A")
elif secret <= 80 and secret > 60:
print("B")
elif secret > 40 and secret <= 60:
print("C")
else:
print("D")
页:
[1]