[Tutor] Re: Help understanding code

Andrei project5 at redrival.net
Mon Oct 20 11:11:43 EDT 2003


Lisa Sullivan wrote:
> I am new to programming and I'm trying to teach myself Python via online 
> tutorials.  I am
> struggling with functions.  I wonder if someone could help me.

Functions are just chunks of code which have a name and may return a result.

> Here is the URL for the program I am trying to understand:
> http://www.honors.montana.edu/~jjc/easytut/easytut/node10.html#SECTION001030000000000000000 

<snip>

>  if get_questions(questions[index]):
> I don't understand this line.

> It seems to move through the list stored in (questions) and determine 
> whether or not to increase
> right by 1.

check_question asks the question, records the answer and compares the 
given answer to the correct one: if answer==given_answer. If the answer 
is correct, the function returns True (that snippet is old, in recent 
Python versions you should just use True and False, there's no need to 
define your own true/false). If the answer is incorrect it returns a False.

In run_test, "if check_question(question[index])" is then basically 
evaluated as "if the value returned by check_question is True, do the 
thing below".
In other words, if check_question returns a value which is evaluated as 
True in a boolean sense (most things are True except for 0, "", and 
other empty things), "right" is increased. If check_question returns a 
false, it does nothing.

> Right should only be increased if the question was answered right, so:
> That must be what the return statements in the check_question function 
> are for.

return statements stop the execution of the function and return whatever 
comes after them on the same line. check_question returns true (1) if 
the given answer is the same as the correct answer and false (0) otherwise.

> It replaces the words get_questions with the value returned from running 
> get_questions.

Yep.

> Is index used to get the index variable we set above?

index *is* the index variable set above.

> If so, then right will only be increased:
> if 0 or 1(questions[current question]

I don't understand this notation. It must be wrong, there is no "or" in 
its interpretation.

> In other words if the current question is answered correctly or 
> incorrectly.

Nope, see my explanation above.

> This doesn't make sense to me.
> I don't understand how the program is progressing through the questions.
> I don't see how it knows to skip the answers.

It doesn't skip the answers. It passes to check_question a list which 
looks like this:

["somequestion", "correctanswer"]
   ^-- index 0       ^-- index 1

In check_question, this is split into a question part and an answer part:

question = question_and_answer[0] # becomes "somequestion"
answer = question_and_answer[1] # becomes "correctanswer"

> I know it has something to do with the fact that the questions and 
> answers are stored in lists
> that are inside of the main list.

Yep. The main list contains 3 question-answer lists. The while loop in 
run_test accesses these items one by one, by increasing the index it's 
looking at the end of each loop. During each loop the question-answer 
list at the current index is passed to check_question and evaluated.

Yours,

Andrei





More information about the Tutor mailing list