First python program, syntax error in while loop

John Gordon gordon at panix.com
Fri May 3 16:04:00 EDT 2013


In <af59d0cb-5ca3-42ab-83c3-2be84da8015d at googlegroups.com> ryankoch38 at gmail.com writes:

> I've got it working! I'm really enjoying python :) But now i'd like to make it break out of the while loop when the user guesses more than 5 numbers and fails

The "Congratulations" message is inside the while loop; that's why it
always prints.

To break after five guesses, you could change the while loop to this:

    while (guess != number) and (tries < 5): 

And then after the while loop, put this if statement:

    if (tries < 5):
        print "\nCongratulations! You guessed my number in", tries, "tries"

    else:
        print "Sorry, you lost!"

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"




More information about the Python-list mailing list