#(a) Write a program that asks the user the same single, simple, random math problem until they get it right. (1A, 1C)
#(b) Add the option for the user to specify how many problems they want to answer. (1A)
#(c) Don't do a fixed number of questions. Instead, ask the user at the end of each question sequence (i.e., when they finally get it right) if they want to continue. (1C)
#(d) Keep track of the user's attempts and the total number of questions, and then report their score (i.e., "you took X tries to answer Y questions correctly") (1A)
#(e) Set a maximum number of tries for each question. When the user makes their last guess, tell them the answer and move on. (1A)
import random
def generate_problem():
a, b = round(random.random() * 10), round(random.random() * 10)
r = round(a + b)
return a, b, r
def a(limit=-1):
x, y, z = generate_problem()
tries = 0
while limit != 0:
tries += 1
if int(input(f"What is {x} + {y}?\n")) == z:
break
limit -= 1
if limit == 0:
print(f"Correct answer is {z}")
return tries
def b(limit=-1):
n = input("How many problems do you want to answer?\n")
for i in range(int(n)):
a(limit)
def c(limit=-1):
n = input("How many problems do you want to answer?\n")
tries = 0
for i in range(int(n)):
tries += a(limit)
if (i != int(n) - 1) and (input("Do you want to continue? (Y/N)\n") == "N"):
break
return tries, n
def d(limit=-1):
tries, total = c(limit)
print(f"You took {tries} tries to answer {total} questions correctly\n")
# Has to be called with the number of allowed attempts, while this argument is optional for other functions
def e(limit):
d(limit)
if __name__ == '__main__':
e(2)
Comments
Leave a comment