Newb ??
jmdeschamps at gmail.com
jmdeschamps at gmail.com
Tue Nov 8 22:14:54 EST 2005
mensanator at aol.com wrote:
> 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")
here is your corrected version:
##FROM here it's good
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
## Here you always get 100, randrange is from start to end+1
the_number = random.randrange(0,101)
##This is OK
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:
## Missing break to explicitly leave loop after 5
break
## Then test to see if last guess equals the_number
if guess != the_number:
print "Sorry you lose."
print "The Correct Number was ", the_number
else:
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