[Tutor] help on newbie program

Nick Jensen njensen@utahfirst.com
Wed May 28 16:52:02 2003


Hello,

	I'm just starting to learn programming and python is my first language. =
I've been using Josh Cogliati's guide: "Non-Programmers Tutorial For =
Python" with a url of =
http://www.honors.montana.edu/~jjc/easytut/easytut/node10.html which is =
linked off from www.python.org under beginners guide. I have read every =
example and line of code up to and including the section on lists, but I =
just cannot understand the last example in that section named test.py.=20

I understand parts of it, but not all of it as a whole. I'm not able to =
follow the flow of the program and whats happening at each step. Maybe =
I'm asking too much, but was wondering if anyone could walk me through =
the code and whats happening. By the way, I did send an email to Josh =
asking this same question, but not sure if I'll hear back or not and =
thought I'd ask here just in case. Here it is...

Thanks!

test.py=20
## This program runs a test of knowledge

true =3D 1
false =3D 0

# First get the test questions
# Later this will be modified to use file io.
def get_questions():
    # notice how the data is stored as a list of lists
    return [["What color is the daytime sky on a clear day?","blue"],\
            ["What is the answer to life, the universe and =
everything?","42"],\
            ["What is a three letter word for mouse trap?","cat"]]
# This will test a single question
# it takes a single question in
# it returns true if the user typed the correct answer, otherwise false
def check_question(question_and_answer):
    #extract the question and the answer from the list
    question =3D question_and_answer[0]
    answer =3D question_and_answer[1]
    # give the question to the user
    given_answer =3D raw_input(question)
    # compare the user's answer to the testers answer
    if answer =3D=3D given_answer:
        print "Correct"
        return true
    else:
        print "Incorrect, correct was:",answer
        return false
# This will run through all the questions
def run_test(questions):
    if len(questions) =3D=3D 0:
        print "No questions were given."
        # the return exits the function
        return
    index =3D 0
    right =3D 0
    while index < len(questions):
        #Check the question
        if check_question(questions[index]):
            right =3D right + 1
        #go to the next question
        index =3D index + 1
    #notice the order of the computation, first multiply, then divide
    print "You got ",right*100/len(questions),"% right out =
of",len(questions)

#now lets run the questions
run_test(get_questions())