如何重新启动Python代码-重新启动数学测验



我正在用Python进行数学测验,如果用户说";是";对于重新启动问题。这是我的python代码。我7岁了,所以请放松:

from random import randint

class MathQuiz:
def whole_math_quiz(self):
num1 = randint(2, 9)
num2 = randint(3, 10)
product = num1 * num2
ans = int(input(f"What is {num1} X {num2}? "))
if ans == product:
print("Well done! You have got the answer right!")
print("P.S Run the python file again to play again..")
else:
print("You have got it wrong!")
print("P.S Run the python file again to play again..")

您可以在包装函数下调用实际的数学测验。这将允许您继续";播放";基于用户输入。

建议代码:

from random import randint
class MathQuiz:
def whole_math_quiz(self):
while True :
self.actual_math_quiz()
newgame = input ("Do you want to play again ?")
if newgame.lower() not in [ 'y', 'yes']:
break
def actual_math_quiz(self):
num1 = randint(2, 9)
num2 = randint(3, 10)
product = num1 * num2
ans = int(input(f"What is {num1} X {num2}? "))
if ans == product:
print("Well done! You have got the answer right!")
else:
print("You have got it wrong!")

最新更新