Nested structures question

Tim Harig usernet at ilthio.net
Wed Jan 12 14:41:53 EST 2011


On 2011-01-12, Physics Python <physicsandpython at gmail.com> wrote:
> while guess != the_number:

=================================================
>     while tries > 7:
>         if guess > the_number:
>             print "Lower..."
>         else:
>             print "Higher..."
>         guess = int(raw_input("Take a guess: "))
>         tries += 1
=================================================

Think about what happens when this nested loop exits because tries > 7?  It
returns to the outer loop whether or not the actual number was guessed
correctly.  There is no real need for this loop.
     
>     print "You guessed it! The number was: ", the_number
>     print "And it only took you", tries, "tries!\n"

Note that the outer loop ends here without any test to see whether or not
the number was actually guested and there is *nothing* that stops this
outer loop, so it will spin forever.

> print "Wow, you suck at this, you should be able to solve this in 7 attempts or less"
>
> raw_input("Press Enter to exit the program.")

This is never reached.



More information about the Python-list mailing list