我是一個乞討的Python學生創建一個程序來給數學測驗。測驗本身有效,但我遇到的問題是繼續測驗,直到用戶希望通過輸入哨兵值退出。我曾嘗試在幾個地方使用sentinel值,但是程序完全停止或程序螺旋進入無限循環。只需要建議正確放置標記值的位置。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")
你應該看看「破發」的關鍵字,就可以退出循環。 – chatton