The following program runs a math quiz consisting of 10 multiplication problems
ID: 663702 • 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. Don’t 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
import random
correct = 0
check = int(raw_input("Enter number of questions"));
level = raw_input ("Enter your level");
operation = raw_input("mention your operation A for addition, M for Multiplication, D for Divison and S for Subtraction")
if (check < 1):
print " Sorry please re-enter numbe of questions"
else:
for i in range(check):
if (level == "Beginner"):
num1 = random.randint(1, 10)
num2 = random.randint(1, 10)
elif (level == "Intermediate"):
num1 = random.randint(1, 25)
num2 = random.randint(1, 25)
else:
num1 = random.randint(1, 50)
num2 = random.randint(1, 50)
if (operation == "M"):
prod = num1 * num2
elif (operation == "A"):
prod = num1 + num2
elif (operation == "D"):
prod = num1/num2
else:
prod = num1 - num2
ans = int(raw_input("What is " + str(num1) +" "+ operation + " " + 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",check," questions. You got ", correct, " of them right."
val = float(correct/check)
if (val > 0.75):
print "Well done!"
elif (val>0.5):
print "You need more practice"
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.