import random
# initialize the right and wrong counters to 0
right = 0
wrong = 0
# loop until the player wants to stop
while True:
# generate two random numbers between 1 and 10
a = random.randint(1, 11)
b = random.randint(1, 11)
# compute the correct answer
correct_answer = a * b
# get the answer from the player
answer = int(input(f"What is {a} * {b}? "))
# check if the answer is correct
if answer == correct_answer:
# if it is, increment the right counter
right += 1
# print a positive message
print("Correct!")
else:
# if it is not, increment the wrong counter
wrong += 1
# print a negative message
print("Incorrect.")
# ask the player if they want to continue
cont = input("Continue? (y/n) ")
if cont != "y":
# if they don't want to continue, exit the loop
break
# print a message based on the number of right answers
if right == 0:
print("You didn't get any right :(")
elif right == 1:
print("You got one right!")
else:
print(f"You got {right} right!")
Expected output:
What is 9 * 5? 45
Correct!
Continue? (y/n) y
What is 10 * 10? 100
Correct!
Continue? (y/n) y
What is 3 * 1? 3
Correct!
Continue? (y/n) y
What is 5 * 3? 15
Correct!
Continue? (y/n) n
You got 4 right!
This program first imports the random module and initializes the right and wrong counters to 0. It then enters a loop that continues until the player wants to stop. Inside the loop, it generates two random numbers between 1 and 10, computes the correct answer, gets the answer from the player, checks if the answer is correct, and increments the right counter or the wrong counter accordingly. It also asks the player if they want to continue and exits the loop if they don't. Finally, it prints a message based on the number of right answers.
0 Comments