# This program presents random addition or multiplication # problems to the user using numbers from 1-12. # The correct answers are calculated using lambda expressions. import random # Asks the user to solve the given number of problems. # 'operator' is a lambda function to calculate the right answer. # 'text' is a string to represent the operator, like "*". def quiz_problems(num_problems, text, operator): correct = 0 for i in range(num_problems): x = random.randint(1, 13) y = random.randint(1, 13) answer = operator(x, y) response = int(input(str(x) + text + str(y) + " = ")) if response == answer: print("you got it right") correct += 1 else: print("incorrect...the answer was", answer) print(correct, "of", num_problems, "correct") print() def main(): quiz_problems(3, " + ", lambda x, y: x + y) quiz_problems(3, " * ", lambda x, y: x * y) main()