(Repeat additions) Listing 5.4, SubtractionQuizLoop.py, generates five random subtraction questions. Revise the program to generate ten random addition questions for two integers between 1 and 15. Display the correct count and test time.
import time
import random
correctCount = 0 #Count the number of correct answers
count = 0 #Count the number of questions
startTime = time.time()*1000.0
while (count < 5):
number1 = random.randint(1,16)
number2 = random.randint(1,16)
#Prompt the student to answer "what is number1 – number2?"
print("What is {} - {} ? ".format(number1,number2))
answer = int(input())
#Grade the answer and display the result
if (number1 - number2 == answer):
print( "You are correct!")
correctCount=correctCount+1
else:
print("Your answer is wrong.\n {} - {} should be".format(number1,number2), (number1 - number2))
#Increase the count by incremeting variable called "count"
count=count+1
print("Correct count is ",correctCount)
endTime = time.time()*1000.0
print("Time spent is {} seconds".format((endTime - startTime) / 1000))
Output:
What is 4 - 14 ?
-10
You are correct!
What is 4 - 9 ?
-5
You are correct!
What is 16 - 4 ?
12
You are correct!
What is 7 - 13 ?
5
Your answer is wrong.
7 - 13 should be -6
What is 1 - 6 ?
4
Your answer is wrong.
1 - 6 should be -5
Correct count is 3
Time spent is 28.8054658203125 seconds