Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Python program that creates a repeating 10 question math quiz that retu

ID: 3688893 • Letter: C

Question

Create a Python program that creates a repeating 10 question math quiz that returns the user's score upon completion. Upon quiz completion, the user can then choose to repeat the math quiz. Each question of the quiz should consist of three parts:

1.an operand x, a random integer between 1 and 20 inclusive

2.an operator, randomly chosen to be +, -, //, *, or %

3.an operand y, a random integer between 1 and 20 inclusive

The following is required:

Use a function that creates a math question and prints the question to screen. Then the function will accept user input as an answer. The function returns True if the user gets the question right and False if the question is answered incorrectly

Pose an initial prompt that asks if the user wants to play the quiz

Use a loop to ask 10 questions and record the number of correct answers for that quiz

Print the number of correct answers at the end of the quiz

Prompt the user to play again

Use a loop to create multiple math quizzes if the user wishes to play again after each quiz. If the user declines to take quiz, the program ends

Your program must use for and while loops accurately and efficiently

To create a random integer first you must import the random number generator using the following code at the top of your program:

    from random import randint

To use the random number generator, look up how to use the 'randint()' function.

Explanation / Answer

import random
import operator

def randomCalc():
ops = {'+':operator.add,
'-':operator.sub,
'*':operator.mul,
'/':operator.truediv}
num1 = random.randint(0,20)
num2 = random.randint(1,20) # didn't sample 0's to protect against divide-by-zero
op = random.choice(list(ops.keys()))
answer = ops.get(op)(num1,num2)
print('What is {} {} {}? '.format(num1, op, num2))
return answer

def askQuestion():
answer = randomCalc()
guess = float(input())
return guess == answer


def quiz():
while True:
input = raw_input('Do you want to play the quiz, y or n? ')
if input == 'n':
break
print('Welcome. This is a 10 question math quiz ')
score = 0
for i in range(10):
correct = askQuestion()
if correct:
score += 1
print('Correct! ')
else:
print('Incorrect! ')
print 'Your score was {}/10'.format(score)


quiz()