Nested structures question

Physics Python physicsandpython at gmail.com
Wed Jan 12 14:52:33 EST 2011


Thanks,

Is this an indentation problem then?
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:

--- start---
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:
    guess = int(raw_input("Take a guess: "))
    tries += 1
    if guess > the_number:
        print "Lower..."
    elif guess < the_number:
        print "Higher..."
    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

raw_input ("\n\nPress the enter key to exit.")

--- end ---

But the book states that this can be done without needing to use breaks.

Thanks!



More information about the Python-list mailing list