我需要一些帮助在 python 中重新启动该程序

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

这是我的代码。我在中断并继续时遇到错误。我的目标是能够从头开始重新运行该程序,并且我尝试将其放入一个函数中,该函数最后会回调自身,但那没有用。非常感谢任何帮助找到根据用户输入重新启动程序的方法。

您的问题与 if 条件和 retry 行的选项卡有关。另外 continue 没有正确地循环

尝试:

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

如果你想在输入YES重播时得到ab的新值,你需要把它们放在循环中如下:

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