简体   繁体   English

我需要一些帮助在 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")

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.我的目标是能够从头开始重新运行程序,并且我尝试将其放入最后调用自身的 function 中,但这不起作用。 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.您的问题与if条件和重试行的选项卡有关。 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:如果您想在输入 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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