Vishnu Menon
Vishnu Menon

Reputation: 3

I need some help restarting this program in python


a=random.randint(0,1000)
b=random.randint(0,1000)

goodQuotes = ["Nice job", "Keep going", "Excellent", "Great job", "You are doing great"]

badQuotes = ["You got that wrong", "Unfortunately that is incorrect", "Try again", "Retry this problem", "You made a mistake"]

while True:
  if (a > b): 
    print('What is' ,a,'-' ,b,)
    answer=int(input("Enter your answer "))
    if int(answer)== a-b:
      print(random.choice(goodQuotes))
    else: 
      print(random.choice(badQuotes))

  if (a < b): 
    print('What is' ,a,'+' ,b)
    if int(answer)== a+b:
      print(random.choice(goodQuotes))
    else: 
      print(random.choice(badQuotes))
 
  if (a == b): 
    print('What is' ,a,'*' ,b)
    if int(answer)== a*b:
      print(random.choice(goodQuotes))
    else: 
      print(random.choice(badQuotes))

retry = input("Want to try another problem, enter yes or no: ")

if 'yes' in retry:
  continue
elif 'no' in retry:
  print("Good Bye")
  break
else:
  print("Invalid Input")

Here is my code. I am getting an error on break and continue. My goal is to be able to rerun the program from its beginning, and I've tried putting it in a function that calls back to itself at the end, but that didn't work. Any help to find a way to restart the program based on user input would be much appreciated.

Upvotes: 0

Views: 66

Answers (1)

user6223604
user6223604

Reputation:

your problem is related to the tab of if condition and retry line. Also continue not properly in loop

Try :

import random
a=random.randint(0,1000)
b=random.randint(0,1000)

goodQuotes = ["Nice job", "Keep going", "Excellent", "Great job", "You are doing great"]

badQuotes = ["You got that wrong", "Unfortunately that is incorrect", "Try again", "Retry this problem", "You made a mistake"]

while True:
    if (a > b): 
        print('What is' ,a,'-' ,b,)
        answer=int(input("Enter your answer "))
        if int(answer)== a-b:
            print(random.choice(goodQuotes))
        else: 
          print(random.choice(badQuotes))

    if (a < b): 
        print('What is' ,a,'+' ,b)
        answer=int(input("Enter your answer "))
        if int(answer)== a+b:
          print(random.choice(goodQuotes))
        else: 
          print(random.choice(badQuotes))
    if (a == b): 
        print('What is' ,a,'*' ,b)
        if int(answer)== a*b:
          print(random.choice(goodQuotes))
        else: 
          print(random.choice(badQuotes))

    retry = input("Want to try another problem, enter yes or no: ")
    if 'yes' in retry:
        continue
    elif 'no' in retry:
        print("Good Bye")
        break
    else:
        print("Invalid Input")

If you want to get new values for a and b when you enter YES to replay, you need to put them in loop as below :

import random

goodQuotes = ["Nice job", "Keep going", "Excellent", "Great job", "You are doing great"]

badQuotes = ["You got that wrong", "Unfortunately that is incorrect", "Try again", "Retry this problem", "You made a mistake"]

while True:
    #To get new values
    a=random.randint(0,1000)
    b=random.randint(0,1000)
    if (a > b): 
        print('What is' ,a,'-' ,b,)
        answer=int(input("Enter your answer "))
        if int(answer)== a-b:
            print(random.choice(goodQuotes))
        else: 
          print(random.choice(badQuotes))

    if (a < b): 
        print('What is' ,a,'+' ,b)
        answer=int(input("Enter your answer "))
        if int(answer)== a+b:
          print(random.choice(goodQuotes))
        else: 
          print(random.choice(badQuotes))
    if (a == b): 
        print('What is' ,a,'*' ,b)
        if int(answer)== a*b:
          print(random.choice(goodQuotes))
        else: 
          print(random.choice(badQuotes))

    retry = input("Want to try another problem, enter yes or no: ")
    if 'yes' in retry:
        continue
    elif 'no' in retry:
        print("Good Bye")
        break
    else:
        print("Invalid Input")

Upvotes: 1

Related Questions