[英]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.