Newb ??

mensanator at aol.com mensanator at aol.com
Tue Nov 8 22:08:25 EST 2005


Chad Everett wrote:
> Hi all,  I am new to the group.  Trying to learn Python programming on my
> own.  I am working through Michael Dawson's Book Python Programming for the
> absolute beginner.
>
> I am tring to write a program that is a number guessing game.  I want to be
> able to give the user 5 tries to guess the number before the program ends.
>
> I get the result that I want when the user misses after the 5th try.  But it
> does not go down to my closing statement to end.  Rather is askes for
> another guess.

Because even after 5 tries, guess != the_number. You won't get past
the while loop until it does. When there have been 5 misses, try
setting
guess=the_number.

>
> I appreciate any help you can give.  If  this is not the correct group for
> these types of questions, I apologize and hope that you can direct me to
> another NG.
>
> Thanks,
> Chad
>
> import random
>
> print "\tWelcome to 'Guess my Number'!"
> print "\nI'm Thinking of a Number between 1 and 100."
> print "\nTry to Guess it in as few attempts as possible.\n"
>
> #Set the initial values
> the_number = random.randrange(100) + 1
> guess = int(raw_input("Take a guess:"))
> tries = 1
>
> #Guessing Loop
>
> while (guess != the_number):
>     if (guess >the_number):
>         print "Lower..."
>     else:
>         print "Higher..."
>     guess = int(raw_input("Take another Guess:"))
>     tries +=1
>     if tries == 5:
>  print "Sorry you lose."
>  print "The Correct Number was ", the_number
>
>
> print "You guess correctly!!"
> print "The number was:", the_number
> print "You got it correct in", tries, "tries"
> 
> raw_input("Press Enter to exit the program")




More information about the Python-list mailing list