[Tutor] While and for loops

A.T.Hofkamp a.t.hofkamp at tue.nl
Tue Jul 14 09:45:29 CEST 2009


Todd Matsumoto wrote:
> Okay,
> 
> I'm not sure if this is good practice, but I could assign a variable within the while loop, that is assigned something that will then break the outer loop.
> 
> while True:
>     breakout = True
>     <do something>
>     for i in items:
>         if i > 10:
>             breakout = False
>         else:
>             <do something>
>     if break is False:
>         break

The statement "if break is False:" is wrong.

Your variable is called "breakout" not "break". In fact, you cannot have 
"break" as variable, the above code will not be accepted by the Python 
interpreter.

There are also a few improvements possible:

You want to use "==" for comparing values. The "is" that you use compares 
objects rather than values.

Also, comparing boolean values with True or False in a condition is not 
needed, you can use the boolean value directly. In this case "not breakout" 
will give you the same result.


Albert


More information about the Tutor mailing list