Answer:
import random
class Arithmetics(object):
trial = 2
score = 0
is_end = "Y"
def start(self):
while self. is_end == "Y":
self. level1()
self. level2()
self. level3()
self. is_end = input("Do you want to play again? Y/N: "). upper()
quit()
def level1(self):
print("--------------------Level 1--------------------\n\n Addition:")
for _ in iter(list, 0):
num1 = random.randint(1,5)
num2 = random.randint(1,5)
if self. score == 5:
print("Congrat! advance to next level\n")
self. init_()
break
print(f"{num1} + {num2}")
result = int(input("Your answer: "))
if result == num1 + num2:
self. increase_score()
elif self. trial == 0:
print("Sorry dear, you failed to pass the text.")
self. end_game()
else:
self. trial_reduce()
def level2(self):
print("--------------------Level 2--------------------\n\n Addition again:")
for _ in iter(list, 0):
num1 = random. randint(1,9)
num2 = random. randint(1,9)
if self. score == 5:
print("Congrat! advance to next level\n")
self. init_()
break
print(f"{num1} + {num2}")
result = int(input("Your answer: "))
if result == num1 + num2:
self. increase_score()
elif self. trial == 0:
print("Sorry dear, you failed to pass the text.")
self. end_game()
else:
self. trial_reduce()
def level3(self):
print("--------------------Level 3--------------------\n\n Subtraction:")
for _ in iter(list, 0):
denum = random. randint(6,9)
num = random. randint(1,5)
if self. score == 5:
self. init_()
self. end_game()
break
print(f"{denum} - {num}")
result = int(input("Your answer: "))
if result == denum - num:
self. increase_score()
elif self. trial == 0:
print("Sorry dear, you failed to pass the text.")
self. end_game()
else:
self. trial_reduce()
#classmethod
def trial_reduce(cls):
cls. trial -= 1
#classmethod
def increase_score(cls):
cls. score += 1
#classmethod
def init_(cls):
cls. trial = 2
cls. score = 0
def end_game(self):
print("Congrats kiddo! You are a math wiz.")
game = Arithmetics()
game. start()
Explanation:
The python class defines the arithmetics class with three-level mathematics for children defined as methods for the class. There are three class methods namely; 'trial_reduce', 'increase_score', and 'init_' with each of them decreasing the trial attribute, increasing the score attribute by one, and initializing the class attributes to its initial state.
The 'end_game' prints a message at the end of the program. Each level is in a loop and counts five times if the answers are correct.