[Tutor] Why won't it enter the quiz?
dyoo at hkn.eecs.berkeley.edu
Sat Sep 17 22:22:54 CEST 2005
On Sat, 17 Sep 2005, Nathan Pinno wrote:
> I can see that I was wrong. I was tired, and I don't always think too
> smart. It should be cq <= q. :)
Here are some general tips about debugging while loops. If we have a
and if we're not entering the loop at all, we should pay particular
attention to 'some_condition', because it's either backwards or checking
the wrong thing.
One technique that's often use is to "log" the variables used in a while
loop, at least temporarily while we're trying to figure out what's going
on in the code. For example, we might want to do something like:
print "About to enter the loop"
print "cq ==", cq
print "q ==", q
print "cq <= q", cq <= q
while cq <= q:
The idea is that we can temporarily make the program do a bit more than
what it should: we add some debugging scaffolding code. Just as buildings
that are being constructed have supports to keep them up, we often write
additional log statements in our programs to give us debuggging
information. It's a little cheezy sometimes, but it's surprisingly
A more rigorous way of adding this scaffold is to use an "assertion". We
can talk about assertions if you'd like.
Symmetrically, if we have:
and if we never get out of the while loop ever (infinite loop), we need to
look at some_condition, and ask ourselves: does anything in the body make
it possible for some_condition to be False?
I'm trying to point out that there are things that you can do to figure
out why programs don't work: those techniques are just as valuable ---
perhaps more so --- than actually writing the program.
More information about the Tutor