The following program runs a math quiz consisting of 10 multiplication problems
ID: 663537 • Letter: T
Question
The following program runs a math quiz consisting of 10 multiplication problems involving operands between 1 and 10:
import random
correct = 0
for i in range(10):
num1 = random.randint(1, 10)
num2 = random.randint(1, 10)
prod = num1 * num2
ans = int(raw_input("What is " + str(num1) + " times " + str(num2) + "? "))
if ans == prod:
print "That's right -- well done. "
correct += 1
else:
print "No, I'm afraid the answer is ", prod, " "
print " I asked you 10 questions. You got ", correct, " of them right."
print "Well done!"
Please help me do the following:
Modify the program so that the user can choose how many questions they will be asked. Dont allow them to choose 0 or a negative number.
Add levels to the program:
o Beginner - with operands between 1 and 10
o Intermediate - with operands between 1 and 25
o Advanced - with operands between 1 and 50
o The user should choose the level when starting a quiz.
Modify the message at the end so that it says:
o Well done! if the user answered more than 3/4 of the questions correctly.
o You need more practice if they get between 1/2 and 3/4 of the questions correct.
o Please ask your math teacher for help! if they get less than 1/2 of the questions correct.
(Round up if in doubt the user needs to answer 5 questions to get of 6 correct)
Allow the user to start another quiz without restarting the program.
Let the user choose the question type: addition, subtraction, multiplication, or mixed (randomly choose for each question).
This is in Python, using IDLE. If possible, please help me come up with a short, clean, and elegant solution.
Explanation / Answer
Sure!! I'll try my best to give a brief yet consistent solution!
import random
input();
restart = raw_input(print"Do you wish you to continue?(Yes/ No)"))
if restart = "Yes":
input();
else:
break;
def input():
level = raw_input("Please enter which level you wish to answer- Beginner, Intermediate or Advanced")
questions = int(raw_input("Please enter how many questions you wish to answer'))
if question <= 0:
print "Enter valid number of questions"
type = raw_input("Please enter what type of questions you answer- Addition, Subtraction, Multiplication or Mixed")
Quiz(level,questions,type);
def Quiz(level, questions, type):
if level == "Beginner":
n=10
else if level == "Intermediate":
n=25
else if level == "Advanced":
n=50
else:
print("Enter a valid level")
for i in range(questions):
num1 = random.randint(1, n)
num2 = random.randint(1, n)
if type == "Addtion":
Addition(num1,num2);
if type == subtraction:
Subtraction(num1,num2);
if type == "Multiplication":
Multiplication(num1,num2);
if type == "Mixed"
ran = random(1,3);
if n==1:
Addition(num1, num2);
if n==2:
Subtraction(num1,num2);
if n==3:
Multiplication(num1,num2) ;
def Addition(num1,num2):
sum= num1 * num2
ans = int(raw_input("What is " + str(num1) + " + " + str(num2) + "? "))
if ans == prod:
print "That's right -- well done. "
correct += 1
else:
print "No, I'm afraid the answer is ", sum, " "
Compute(correct);
def Subtraction(num1,num2):
diff = num1 * num2
ans = int(raw_input("What is " + str(num1) + " times " + str(num2) + "? "))
if ans == diff:
print "That's right -- well done. "
correct += 1
else:
print "No, I'm afraid the answer is ", diff, " "
Compute(correct);
def Multiplication(num1,num2):
prod = num1 * num2
ans = int(raw_input("What is " + str(num1) + " times " + str(num2) + "? "))
if ans == prod:
print "That's right -- well done. "
correct += 1
else:
print "No, I'm afraid the answer is ", prod, " "
Compute(correct);
def Compute(correct):
if correct >= floor(3/4*questions):
print"Well Done!!"
else if correct < floor(3/4*questions) && correct > floor(1/2*questions):
print"More practice required!"
else
print"Please ask your math teacher for help!"
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.