Python Bibliotheca
Python resources for teachers and students.

Math Quiz

Description

The following program runs a math quiz consisting of 10 multiplication problems involving operands between 1 and 10:

from random import randint 

correct = 0

for i in range(10):
    n1 = randint(1, 10)
    n2 = randint(1, 10)
    prod = n1 * n2
    
    ans = input("What's %d times %d? " % (n1, n2))
    if ans == prod:
        print "That's right -- well done.\n"
        correct = correct + 1
    else:
        print "No, I'm afraid the answer is %d.\n" % prod

print "\nI asked you 10 questions.  You got %d of them right." % correct
print "Well done!"

Your mission will be to do the following:

  1. Modify the program so that the user can choose how many questions they will be asked.

  2. Add levels to the program:
    • Beginner - with operands between 1 and 10
    • Intermediate - with operands between 1 and 25
    • Advanced - with operands between 1 and 100

  3. Modify the message at the end so that it says:
    • Well done!: if the user answered more than 2/3 of the questions correctly.
    • You need more practice: if they get between 1/3 and 2/3 of the questions correct.
    • Please ask your math teacher for help!: if they get less than 1/3 of the questions correct.

  4. Allow the user to start another quiz without restarting the program.

  5. Let the user choose the question type: addition, subtraction, multiplication, or mixed.

Extra for Experts:

  1. Add division as a question type, but only generate questions which have integral solutions.

  2. Maintain session totals for each type of question (the total number of questions, and the total answered correctly; the total number of addition, subtraction, multiplication, and division questions and the total correct for each of these question types).

[ Copyright 2006, Jeffrey Elkner ]

Comments, questions, or suggestions? Email the webmaster