Search code examples
pythonsentinel

Python Sentinel Value


I am a begging Python student creating a program to give a math quiz. The quiz itself works but the problem I am having is continuing the quiz until the user desires to quit by entering the sentinel value. I have tried using the sentinel value in several places but either the program stops completely or the programs spirals into an infinite loop. Just need advice on where to properly place the sentinel value.

 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")

Solution

  • See if the following works

    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")