2017-04-20 91 views
0

我是一個乞討的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") 
+1

你應該看看「破發」的關鍵字,就可以退出循環。 – chatton

回答

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

正是我需要完成的!謝謝 – WXHXIXTE

0

如前所述休息可以是有益的在這裏,

While True: 
    flag = int(input()) 
    if(flag < 1): 
     # This will break the While Loop, thus exiting the program 
     break