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. > > 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") > > I don't personally think the two possible solutions you've been given are very pythonic. All you really need to do to get the loop termination conditions right (he says confidently, and with absolutely no testing at all) is change the while line to
while guess != the_number and tries <=5: but there are other things wrong with the program. In "pseudo-code" (stuff that helps you think about the problem without being able to feed it into an interpreter) your solution should be while not guessed and guesses < 5 guess number if guessed congratulate user else crow and rub user's face in it At present your code will always tell the user she guessed correctly, even after it's told her she lost. See if this (tested) rewrite makes sense to you: 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 and tries < 5: if (guess >the_number): print "Lower..." else: print "Higher..." guess = int(raw_input("Take another Guess:")) tries +=1 if guess == the_number: print "You guess correctly!!" print "The number was:", the_number print "You got it correct in", tries, "tries" else: print "Sorry you lose." print "The Correct Number was ", the_number regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC www.holdenweb.com PyCon TX 2006 www.python.org/pycon/ -- http://mail.python.org/mailman/listinfo/python-list