mchermside at ingdirect.com
Fri Mar 14 14:09:42 CET 2003
David Bear writes:
> I think I'm confusing myself regarding semantics for a while loop.
> while (conditionX):
> do something
> while (conditionY):
> do something else
> while (conditionZ):
> do something
> here conditionX becomes False
> print something
> print something else
> Will the outer while loop cause all nested whiles to end as soon as
> conditionX is false? or will 'print something' be reached, then 'print
> something else' and so on untill it reaches the outer scope?
I think you are missing out on one of Python's most powerful features.
Yes, the c.l.py newsgroup will answer your questions in a polite and
helpful fashion, but we've ALSO got something called the "python
interpreter" <wink> which can answer this kind of question far faster than
It works like this. From your command line, type "python". You will then
get a header, then a prompt, like this:
Type things at the prompt, and it will automatically (and far faster than
a newsgroup) show you what Python would do in this situation. For
example, to answer your question above, here's what I would type:
>>> conditionX = True
>>> conditionY = True
>>> conditionZ = True
>>> def doSomething():
>>> def doSomethingElse():
>>> while conditionX:
... while conditionY:
... while conditionZ:
... conditionX = False
... print 'something'
... print 'something else'
After I hit return one more time it starts printing out "something"
over and over without stopping. So that immediately answers your
first question... "print something" _IS_ reached. Apparently "print
something else" is _NOT_ reached (perhaps because "while conditionZ"
Notice how this actually required LESS typing than posting the
question... isn't this interpreter thing a neat tool?
-- Michael Chermside
PS: Please don't take offense... I wrote this as if you'd never heard
of the interpreter more for humor value than because I actually
thought you hadn't heard of it (and now I'm ruining the humor by
pointing it out :-( ). And we really DON'T mind getting occasional
questions here at c.l.py which people could have easily answered
on their own (note the word "occasional"). But there really _IS_
a lesson lurking here... if you come to Python from a different
kind of programming language (or from no programming background at
all) you may be used to a system where the only sensible way to
figure out how things work is to spend lots of time reading
through the manuals, or ask other people. But in Python it's often
easiest to just TRY IT, because the difficulty of trying it is
so small, and the chance of its answering your question is so
large. And when that fails, you can still come back to c.l.py for
More information about the Python-list