Nested structures question

Tim Harig usernet at ilthio.net
Wed Jan 12 15:12:11 EST 2011


[wrapped lines to <80 characters per RFC 1855]
On 2011-01-12, Physics Python <physicsandpython at gmail.com> wrote:
> Is this an indentation problem then?

That depends how you look at it.  I was not clear from your code exactly
where you wanted to handle things.

> How do I update the sentinel within the secondary while loop. I am
> trying to avoid using breaks by the way, as I can program this example
> using breaks:

You don't need breaks.

> import random
> print "\tWelcome to 'Guess my number'!:"
> print "\nI'm thinking of a number between 1 and 100."
> print "Try to guess it in as few attempts as possible.\n"
>
> the_number = random.randrange(1,101)
>
> tries = 0
>
> while True:

while can be used to test for more then a single condition at a time using
and/or chains.

>     else:
>         print "You guessed it! The number was", the_number
>         print "And it only took you", tries, "tries!\n"
>         break
>     if  tries == 7:
>         print "Wow you suck! It should only take at most 7 tries!"
>         break

Both of these tests can be performed as part of the loop itself.  The end
results can therefore be tested and handled outside of the loop without
using breaks.



More information about the Python-list mailing list