[Edu-sig] source code from SA:10648

kirby urner kirby.urner at gmail.com
Fri Jul 16 06:48:53 CEST 2010

On Thu, Jul 15, 2010 at 7:37 PM, Tim Peters <tim.peters at gmail.com> wrote:

<< snip >>

> In fact, if you don't mind permuting the quiz each time, there's no
> need to make a copy of the quiz then either (because nothing is ever
> removed from it):
> def askq(quiz = quiz1):
>     shuffle(quiz)
>     for q, a in quiz:    # marches over the questions in a random order
>         # `q` is the question, `a` is the answer

Yep, Tim's way is so much more Pythonic.

Here's a new version for tomorrow's class:

def askq2(quiz = quiz1):
    score = 0
    possible = len(quiz)
    for question, answer in quiz:
        user_answer = raw_input("Your answer? ")

        if user_answer.upper() == answer.upper():
            # yes and Yes would both count (but not Y)
            score = score + 1
            print("Correct answer was %s" % answer)

    print("Your score was %s out of a possible %s" % (score, possible))



More information about the Edu-sig mailing list