[英]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")
这是我的代码。 我在休息时遇到错误并继续。 我的目标是能够从头开始重新运行程序,并且我尝试将其放入最后调用自身的 function 中,但这不起作用。 任何帮助找到基于用户输入重新启动程序的方法将不胜感激。
您的问题与if条件和重试行的选项卡有关。 在循环中也不能正确地继续
尝试:
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 重放时获取a和b的新值,则需要将它们放入循环中,如下所示:
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.