简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM