Jeff Elkner’s Python Programming Problems

Math Quiz


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
        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).

Jeff Elkner’s Python Programming Problems

Blogged with the Flock Browser
Anuncio publicitario

Publicado el 5 agosto 2009 en Python y etiquetado en , . Guarda el enlace permanente. Deja un comentario.

Deja una respuesta

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de

Estás comentando usando tu cuenta de Salir /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Salir /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Salir /  Cambiar )

Conectando a %s

A %d blogueros les gusta esto: