Python 哨兵值
Python Sentinel Value
我是一个乞讨的 Python 学生,正在创建一个程序来进行数学测验。测验本身有效,但我遇到的问题是继续测验,直到用户希望通过输入标记值退出。我曾尝试在几个地方使用哨兵值,但要么程序完全停止,要么程序螺旋式进入无限循环。只需要关于在何处正确放置哨兵值的建议。
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")
如前所述,休息一下可能会有帮助,
While True:
flag = int(input())
if(flag < 1):
# This will break the While Loop, thus exiting the program
break
看看下面的是否有效
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")
我是一个乞讨的 Python 学生,正在创建一个程序来进行数学测验。测验本身有效,但我遇到的问题是继续测验,直到用户希望通过输入标记值退出。我曾尝试在几个地方使用哨兵值,但要么程序完全停止,要么程序螺旋式进入无限循环。只需要关于在何处正确放置哨兵值的建议。
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")
如前所述,休息一下可能会有帮助,
While True:
flag = int(input())
if(flag < 1):
# This will break the While Loop, thus exiting the program
break
看看下面的是否有效
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")