In: Computer Science
Design and write a Python 3.8 program that gives and grades a math quiz. The quiz will give 2 problems for each of the arithmetic operations: +, -, *, /, and %. First, two addition problems will be presented (one at a time) with random numbers between 1 and 20. Then two subtraction problems, multiplication, division and modulus. For division use // and do floor division; for example: for 10//3, the answer should be 3. For an example of the first problem: the program generates two random numbers (let's say num1 is 17 and num2 is 5), and prints out a statement of the problem as 17 + 5 =. The user will enter a correct answer or a wrong answer. If the answer is correct, a message will be printed and one will be added to the number of correct answers. Then the second addition problem is given and checked. Next, two subtraction problems will be given and checked, and so on.
Define a function for each of the 5 operations. Each function will display 2 problems (one at a time) to be solved. The call to each function should return the number of correct answers (0, 1, or 2). After calling all 5 functions in sequence, the main program will print out the total score of that quiz, and asks the user if he/she wants to take another quiz. The main program should keep calling the 5 functions in sequence as long as the user wants to take another quiz.
Insert a screenshot of the programs output
import random
def addition():
score = 0
# 2 questions so loop twice
for i in range(2):
# Find two random numbers
a = random.randint(1,20)
b = random.randint(1,20)
print(a, "+",b,"=",end = " ")
answer = int(input())
# Check if answer is correct
if(answer == a+b):
print("The answer is correct")
score+=1
else:
print("The answer is wrong")
return score
def subtraction():
score = 0
for i in range(2):
# Find two random numbers
a = random.randint(1,20)
b = random.randint(1,20)
print(a, "-",b,"=",end = " ")
answer = int(input())
# Check if answer is correct
if(answer == a-b):
print("The answer is correct")
score+=1
else:
print("The answer is wrong")
# Return number of correct answers.
return score
def multiplication():
score = 0
for i in range(2):
a = random.randint(1,20)
b = random.randint(1,20)
print(a, "*",b,"=",end = " ")
answer = int(input())
if(answer == a*b):
print("The answer is correct")
score+=1
else:
print("The answer is wrong")
return score
def division():
score = 0
for i in range(2):
a = random.randint(1,20)
b = random.randint(1,20)
print(a, "/",b,"=",end = " ")
answer = int(input())
if(answer == a//b):
print("The answer is correct")
score+=1
else:
print("The answer is wrong")
return score
def modulus():
score = 0
for i in range(2):
a = random.randint(1,20)
b = random.randint(1,20)
print(a, "%",b,"=",end = " ")
answer = int(input())
if(answer == a%b):
print("The answer is correct")
score+=1
else:
print("The answer is wrong")
return score
if __name__ == "__main__":
while(True):
total_score = 0
total_score += addition()
total_score += subtraction()
total_score += multiplication()
total_score += division()
total_score += modulus()
print("Your total score is", total_score,"out of 10")
print("Do you want to attempt the quiz again? Enter Y/y for yes and N/n for no")
attempt = input()
attempt.upper()
if(attempt == "N"):
break
I would love to resolve any queries in the comments. Please consider dropping an upvote to help out a struggling college kid :)
Happy Coding !!