我被安排了一项数学测验程序。基本代码是给我的,我被设置了一系列的挑战,使之更复杂。我检查最终分数是否在某个范围内的方法不能正常工作,指令是检查用户的得分是否在66%或更高,如果是这样,程序应该打印“做得好!”。如果用户的分数在33%到66%之间,程序应该打印“你需要更多的练习”,如果他们的分数低于33%,那么程序将打印“请向你的数学老师寻求帮助”。然而,这并没有发生,而且有一些奇怪的结果。感谢任何帮助。在
代码如下:
from random import randint
while True:
correct = 0
userrange = int(input("How many questions would you like?\n"))
difficulty = input("Choose your difficulty: beginner, intermediate or advanced?\n")
if difficulty == "beginner":
no = 10
if difficulty == "intermediate":
no = 25
if difficulty == "advanced":
no = 100
qtype = input("Would you like to do addition, subtraction or multiplication?\n")
if qtype == "addition":
for i in range(userrange):
n1 = randint(1, no)
n2 = randint(1, no)
prod = n1 + n2
lo = "plus"
if qtype == "subtraction":
for i in range(userrange):
n1 = randint(1, no)
n2 = randint(1, no)
prod = n1 - n2
lo = "minus"
if qtype == "multiplication":
for i in range(userrange):
n1 = randint(1, no)
n2 = randint(1, no)
prod = n1 * n2
lo = "times"
ans = int(input("What's %d %s %d?" % (n1 ,lo ,n2)))
if ans == prod:
print ("That's right -- well done.\n")
correct = correct + 1
else:
print ("No, I'm afraid the answer is %d.\n" % prod)
print ("\nI asked you %d questions. You got %d of them right." %(userrange, correct))
if correct >= (userrange%3)*2:
print ("Well done!")
elif (userrange%3) < correct < (userrange%3)*2:
print ("You need more practice")
else:
print ("Please ask your maths teacher for help!")
try_again = int(input("\nPress 1 to try again, press 0 to exit.\n "))
if try_again == 0:
break
你很接近。。。你只需要稍微移动一下循环的位置,这样它就可以捕捉到问题的实际问题。在
现在的情况是,你只是在你产生问题的那部分循环。在
我将在这里发布整个代码,因为有一些缩进更改以支持新循环:
相关问题 更多 >
编程相关推荐