953 views
3 votes
Write a program that teaches arithmetic to a young child. The program tests addition and subtraction. In level 1 it tests only addition of numbers less than 10 whose sum is less than 10. In level 2 it tests addition of arbitrary one-digit numbers. In level 3 it tests subtraction of one-digit numbers with a nonnegative difference. Generate random problems and get the player input. The player gets up to two tries per problem. Advance from one level to the next when the player has achieved a score of five points.

User Chocochaos
by
6.8k points

1 Answer

3 votes

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--------------------\\\\ 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\\")

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--------------------\\\\ 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\\")

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--------------------\\\\ 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()

Step-by-step 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.

User Kaorukobo
by
7.2k points