【发布时间】:2020-09-03 23:52:21
【问题描述】:
条件语句后的打印函数在用户输入导致异常并重新启动程序时打印两次。我没有看到我在哪里调用了两次变量。
# -*- coding: utf-8 -*-
def start():
concept = ("Concept: ")
global grade
while True:
try:
grade = float(input("something: "))
break
except ValueError:
print("please type numbers")
start()
break
finally:
if grade > 100:
print("grade exceeds")
start()
break
elif grade < 0:
print("grade deficit")
start()
break
elif grade >= 90 and grade <= 100:
print(concept + "A")
elif grade >= 80 and grade <= 89:
print(concept + "B")
elif grade >= 70 and grade <= 79:
print(concept + "C")
elif grade >= 60 and grade <= 69:
print(concept + "D")
elif grade >= 0 and grade <= 59:
print(concept + "F")
else:
pass
start()
感谢您提供的任何帮助。
【问题讨论】:
-
不要使用递归进行输入验证。
-
请提供导致错误输出的输入,以及它导致的输出。
-
请提供预期的minimal, reproducible example。显示中间结果与您的预期不同的地方。将您的测试数据作为程序的一部分包含在内,并正确跟踪执行和数据流。请参阅此lovely debugging site 寻求帮助。
-
由于您在
finally:块中打印结果,因此无论是否发生异常都会打印。
标签: python python-3.x function exception conditional-statements