Reputation:
I am working on a Math Quiz in Python and I want the python code for the math quiz to restart if the user says "Yes" to a restart question. Here's my python code. I am 7 so please take it easy:
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..")
Upvotes: 0
Views: 178
Reputation: 352
You could call the actual math quiz under a wrapper function. This will allow you to continue to "play" based on user input.
Suggested code :
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!")
Upvotes: 1