In: Computer Science
----------------------------------------------------------------------------------------------------------------IN
PYTHON-----------------------------------------------------------------------------------------------------------------------------write
a Quiz Generator. This program is expected to generate questions
for a student, record student answers, displays the answers
correctly entered and reports the total time taken by the student
to finish the quiz.
a) The program should initialize three variables, one called
correct that starts with an int of 0, second called count that also
starts with an int of 0 and third called totalquestions that starts
with an int of 5 (Hint: Typecast all the initialized variables to
int values). The variable correct stores the correct answers
entered by the student, count variable counts the number of
questions and totalquestions is a constant value for total number
of questions that equals 5.
b) Using the python time library, start the timer for the student.
Store the value in a variable startTime. (Hint: Use time() method).
Display the message “The quiz has started” using a print
statement.
c) Using randint() method, generate two single digit integers for
each question and store them in num1 and num2 variables. (Hint: use
while to loop through totalquestions, generating two random
integers (between 0-9) for each question)
d) Ask the student to input the answer to "What is num1 ** num2?"
using input() function and save this value in a variable answer.
Grade the answer by comparing its value to the actual operation
between the two numbers and display the correct answers if the
answer entered is wrong. (Hint: The student must enter the answer
for num1 raise to the power num2. Then, after grading, using print
statements, display the message “You are correct!” for correct
answers entered by the student or “You are wrong! Correct answer
is…” for wrong answers entered by the student).
e) End the timer for the student. Store the value in a variable endTime. Use totalTime variable to store the total time taken by the student. (Hint: totalTime is the difference value of endTime and startTime).
f) Print the number of points received based on the number of
answers correctly entered by the student (Hint: You can display
something like this “The number of points received are 3 out of
5”). Also, print the total test time taken by the student in
seconds.
g) Ask input from the user if he wants to retake the test or exit.
Depending upon the option selected by the user, restart the program
again or end it. You can ask the user for input "Do you want to
retake the quiz? Type Y/N: ". If the user enters N or stop or exit,
end the program using ‘break’. If the user types Y, restart the
quiz again. (Hint: use infinite while loop to restart the quiz
again).
h) Use the same variable names and print statement examples as
given in the question.
The Python code for the above program is given below :-
import random
import time
while True:
correct = 0
count = 0
totalquestions = 5
startTime = time.time()
print("The quiz has started")
while count < totalquestions:
count += 1
num1 = random.randint(0, 9)
num2 = random.randint(0, 9)
answer = input("What is {} ** {}? ".format(num1, num2))
if int(answer) == num1 ** num2:
print("You are correct!")
correct += 1
else:
print("You are wrong! Correct answer is {}".format(num1 ** num2))
endTime = time.time()
print("Total Time:{} seconds".format(int(endTime - startTime)))
print("The number of points received are {} out of {}".format(correct, totalquestions))
inp = input("Do you want to retake the quiz? Type Y/N: ")
if inp.lower() == "y" or inp.lower() == "yes":
continue
elif inp.lower()=="n" or inp.lower()=="no":
exit()
Screenshots for above code :-
Output of the above code:-