我是一名乞求Python学生创建一个程序来进行数学测验。测验本身有效,但我遇到的问题是继续测验,直到用户希望通过输入标记值退出。我已尝试在几个地方使用sentinel值,但程序完全停止或程序螺旋形成无限循环。只需要在哪里正确放置哨兵值的建议。
import random
a = random.randint(1,15)
b = random.rantint(1,15)
c = a + b
while flag != -1
print("Enter the sum of", a, "+",b)
d=int(input())
if (c==d):
print("Correct")
else:
print("Incorrect, the correct answer is", c)
flag = int(input("If you would like to continue enter 1 or -1 to quit))
if (flag < 0) :
print ("Quiz complete")
答案 0 :(得分:1)
查看以下内容是否有效
import random
flag = 0
while flag != -1:
a = random.randint(1,15)
b = random.randint(1,15)
c = a + b
print("Enter the sum of", a, "+",b)
d=int(input())
if (c==d):
print("Correct")
else:
print("Incorrect, the correct answer is", c)
flag = int(input("If you would like to continue enter 1 or -1 to quit: "))
if (flag < 0) :
print ("Quiz complete")
答案 1 :(得分:0)
如上所述,休息在这里很有帮助,
While True:
flag = int(input())
if(flag < 1):
# This will break the While Loop, thus exiting the program
break