In: Computer Science
python please take it out from class and make it all functions, add subtraction and multiplication functions with it. than you so much for helping me out. import random image = 'w' class cal(): def __init__(self, a, b): self.a = a self.b = b def add(self): return self.a + self.b a = random.randint(0, 9) b = random.randint(0, 9) obj = cal(a, b) print("0. Exit") print("1. Add") choice = int(input("Enter choice: ")) cnt = 0 # To hold number of tries if choice == 1: while True: print(" what do you think the sum of these two numbers are? ", a, " + ", b) print("This is how it looks viually", a * image, "+ ", b * image) sum = input() answer = int(sum) if answer == obj.add(): print("Perfect, the answer is correct...") cont = input("\nIf you want to solve another question then press 1. If not then any other key ") if cont == "1": a = random.randint(0, 9) b = random.randint(0, 9) obj = cal(a, b) cnt = 0 continue else: break elif answer != obj.add(): print("I am sorry, your answer is wrong Sean,Please Try again:") # Incrementing count cnt = cnt + 1 # Checking count if cnt == 3: # Prompting for next try ans = input( "\nMax number of tries reached. Do you want to try another question or quit? (T/Q): ") # Checking answer if ans.upper() == "T": a = random.randint(0, 9) b = random.randint(0, 9) obj = cal(a, b) continue else: break else: continue
CODE:
import random
image = 'w'
#modified functions which accepts two numbers each and returns the
respective
#output
def add(a,b):
return a + b
def multiply(a,b):
return a*b
def subtract(a,b):
return a-b
a = random.randint(0, 9)
b = random.randint(0, 9)
print("0. Exit")
print("1. Add")
print("2. Multiply")
print("3. Subtract")
choice = int(input("Enter choice: "))
cnt = 0 # To hold number of tries
if choice == 1:
while True:
print(" what do you think the sum of these two numbers are? ", a, "
+ ", b)
print("This is how it looks viually", a * image, "+ ", b *
image)
sum = input()
answer = int(sum)
if answer == add(a,b):
print("Perfect, the answer is correct...")
cont = input("\nIf you want to solve another question then press 1.
If not then any other key ")
if cont == "1":
a = random.randint(0, 9)
b = random.randint(0, 9)
cnt = 0
continue
else:
break
elif answer != add(a,b):
print("I am sorry, your answer is wrong Sean,Please Try
again:")
# Incrementing count
cnt = cnt + 1
# Checking count
if cnt == 3:
# Prompting for next try
ans = input(
"\nMax number of tries reached. Do you want to try another question
or quit? (T/Q): ")
# Checking answer
if ans.upper() == "T":
a = random.randint(0, 9)
b = random.randint(0, 9)
continue
else:
print('Bye!')
break
else:
continue
elif choice == 2:
while True:
print(" what do you think the product of these two numbers are? ",
a, " * ", b)
print("This is how it looks viually", a * image, " * ", b *
image)
#same as addition
sum = input()
#asking for the answer
answer = int(sum)
#using the multiply function
if answer == multiply(a,b):
print("Perfect, the answer is correct...")
cont = input("\nIf you want to solve another question then press 1.
If not then any other key ")
if cont == "1":
#if the user entered correctly
a = random.randint(0, 9)
b = random.randint(0, 9)
cnt = 0
continue
else:
break
#if the user did not answer correctly
elif answer != multiply(a,b):
print("I am sorry, your answer is wrong Sean,Please Try
again:")
# Incrementing count
cnt = cnt + 1
# Checking count
if cnt == 3:
# Prompting for next try
ans = input(
"\nMax number of tries reached. Do you want to try another question
or quit? (T/Q): ")
# Checking answer
if ans.upper() == "T":
a = random.randint(0, 9)
b = random.randint(0, 9)
continue
else:
print('Bye!')
break
else:
continue
elif choice == 3:
while True:
#same as addition
#displaying two random numbers
print(" what do you think the sum of these two numbers are? ", a, "
- ", b)
print("This is how it looks viually", a * image, " - ", b *
image)
sum = input()
answer = int(sum)
#asking for the answer
if answer == subtract(a,b):
print("Perfect, the answer is correct...")
cont = input("\nIf you want to solve another question then press 1.
If not then any other key ")
if cont == "1":
#if the user continues
a = random.randint(0, 9)
b = random.randint(0, 9)
cnt = 0
continue
else:
break
#if the user is wrong
elif answer != subtract(a,b):
print("I am sorry, your answer is wrong Sean,Please Try
again:")
# Incrementing count
cnt = cnt + 1
# Checking count
if cnt == 3:
# Prompting for next try
ans = input(
"\nMax number of tries reached. Do you want to try another question
or quit? (T/Q): ")
# Checking answer
if ans.upper() == "T":
a = random.randint(0, 9)
b = random.randint(0, 9)
continue
else:
print('Bye!')
break
else:
continue
____________________________________________
CODE IMAGES:
_________________________________________________
OUTPUT:
___________________________________________
Feel free to ask any questions in the comments section
Thank You!