To my surprise I'm doing well so far. But I hit a wall with a program I am working on. The program picks a number from 1 to 100 and prompts you to guess the number. If you guess wrong it tells you to guess lower or higher until you get it right. The it congratulates you and ends. I got that part of the program working. Then I had to add a new feature. The program had to be set up so that you only had 5 guesses to get the number. Use up your guesses, and it gives you a you lose message and ends.
I got it to tell you grats you guessed the number in X tries. I got it to tell you you lost after 5 tries. But I have a problem in the code. If you guess the number right without using up your guesses it tells you you win and asks you to guess again. And again. until you use up your guesses. It's stuck making you guess 5 times whether you win or lose.
If any of you know programing, could you take a look at this code and see if you can tell me what needs to be tweaked to make it stop the guess loop after 5 tries? for the purpose of debugging sanity I changed it to guess a number between 1 and 2 in three tries.
Code: Select all
# Guess My Number
#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money
#
#Michael Dawson - 1/8/03 - V 1.0
#
# Klast - V 2.0
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"
# set the initial values
the_number = random.randrange(2) + 1
guess = int(raw_input("Take a guess: "))
tries = 1
# guessing loop
while tries <3:
if (guess == the_number):
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
elif (guess > the_number):
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take a guess: "))
tries += 1
# Results feedback
if tries >=3 :
print "unfortunately you could not guess it in five tries"
raw_input("\n\nPress the enter key to exit.")