Answer
This is an example of the code explained below; it will generate 10 random addition questions.
What is 5+1?
Work Step by Step
import random
import time
correctCount = 0
count = 0
NUMBER_OF_QUESTIONS = 10
startTime = time.time()
while count < NUMBER_OF_QUESTIONS:
number1 = random.randint(0,9)
number2 = random.randint(0,9)
if number1 < number2:
number1, number2 = number2, number1
answer = eval(input("What is " +str(number1) + "+" +str(number2) + "? "))
if number1 + number2 == answer:
print("You are correct!")
correctCount += 1
else:
print("Your answer is wrong.\n",number1, "+",number2, "is", number1 + number2)
count += 1
endTime = time.time()
testTime = int(endTime -startTime)
print("Correct count is", correctCount, "out of", NUMBER_OF_QUESTIONS, "\nTest time is", testTime, "seconds")