[prog] I don't understand this program (-- python) how does this work??

Robert J. Hansen cortana at earthlink.net
Thu Dec 12 23:40:01 EST 2002


> I understand the above section returns a list of questions and answers.

Not quite--it returns a list /of lists/, each list containing one
question and one answer.  You may be tempted to think it's a pedantic
difference, but it's not.

> def check_question(question_and_answer):
> # I don't understand this section at all....I don't know how it works

It's pretty straightforward.  Just look at it one line at a time.

check_question is a function: we know it's a function because we used
the def command.  All functions have inputs and outputs; the input to
this function is a single variable called "question_and_answer".  Right
now, we don't know anything about question_and_answer.  For all we know,
it contains a recipe for potato salad.  It's often very tempting to
think you know what a variable is just because it has a descriptive
name.  Don't fall into this habit--it's a bad one and takes a long time
to unlearn.

As a general rule, you should always give your variables descriptive
names--but you can't count on other people doing likewise.

> 	question = question_and_answer[0]
> 	answer = question_and_answer[1]

Now we're defining two new variables, question and answer.  question
gets assigned the first element in the list question_and_answer; answer
gets assigned the second element.  Since we see question_and_answer is
being used as a list, we can infer question_and_answer is a list.

For instance--if question_and_answer is ["How much do we love our
sysadmin?", "Not enough!"], question would be assigned "How much do we
love our sysadmin?" and answer would be assigned "Not enough!".

Note that while human beings start counting at 1, computers generally
start at 0.  This is purely a cultural distinction--you could start
counting from -48, 16, or 234792341 if you wanted to, and things would
still be self-consistent.  It's just that most languages don't support
this degree of flexibility.  (Some languages, like Ada, do.)

> 	#give the question to the user
> 	given_answer = raw_input(question)

Look up raw_input.  It's another function, which means it has inputs and
outputs.  question is given as input to raw_input, and the output from
raw_input (what it retuns via a "return" statement) is assigned to the
variable given_answer.

What this line will do is print "How much do we love our sysadmin?" and
wait for the user to type something and hit RETURN.  When that happens,
raw_input will return whatever stuff the user entered.

> 	#compare the users answer to the testers answer
> 	if answer == given_answer:
> 		print "Correct"
> 		return true
> 	else:
> 		print "Incorrect, correct answer was",answer
> 		return flase

return false, not flase.  Besides that, this should be self-explanatory.

> 		if check_question(questions[index]):
> 			right = right + 1
> # I don't understandthe above two lines, how does the 
> # check_question(questions[index): work? 

If you've got money, then pay me some; else, pay me later.

See how that works?  If (a certain statement) is true, then (do
something).

Likewise, in Python:

if x:
	do_something
else:
	do_something_else

Python will evaluate x, whatever it is, for a true-or-false value.  If
it's true, it will go on and do whatever sequence of steps is listed. 
If it's not true, it'll skip the sequence and look for an elsif or else
clause.

(Elsif may be elif--sorry, I do a lot of Perl and Python programming
both, and I'm constantly getting the two else-ifs confused.)

So the if statement is evaluating the function check_question, giving it
a certain input value (questions[index]).  If check_question returns
true, then it adds one to right.  If it doesn't... then it goes to the
else clause.





More information about the Programming mailing list