Newbie help

Chad Everett cceverett at gmail.com
Sun Feb 13 22:03:42 EST 2005


Hey guys,

Hope you can help me again with another problem.  I am trying to learn 
Python on my own and need some help with the following.

I am writing a program that lets has the pc pick a number and the user 
has five guess to get the number.
1. BUG:  If the number is say 35 and I guess 41 the program tells me 
that I guessed the correct number and tells me I guessed 31.

2.When I do get the correct number I can not get the program to stop 
asking me for the number.


Your help is greatly appreciated.

Chad

# Five Tries to Guess My Number
#
# The computer picks a random number between 1 and 100
# The player gets Five tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money
#
# Chad Everett 2/10/2005

import random

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100."
print "You Only Have Five Guesses.\n"

# set the initial values
number = random.randrange(100) + 1
guess = int(raw_input("Go Ahead and Take a guess: "))
tries = 1


# guessing loop


while guess != number:

     if (guess > number):
         print "Guess Lower..."
     else:
         print "Guess Higher..."

     guess = int(raw_input("Take Another guess: "))
     tries += 1

     print "You guessed it!  The number was", number
     print "And it only took you", tries, "tries!\n"

     if tries == 5:
         print "Sorry You Lose!!!!"
         print "The Number was ", number

raw_input("\n\nPress the enter key to exit.")

THIS IS WHAT THE RESULTS LOOKS LIKE WHEN I RUN THE PROGRAM

Welcome to 'Guess My Number'!

I'm thinking of a number between 1 and 100.
You Only Have Five Guesses.

Go Ahead and Take a guess: 99
Guess Lower...
Take Another guess: 98
You guessed it!  The number was 85
And it only took you 2 tries!

Guess Lower...
Take Another guess: 44
You guessed it!  The number was 85
And it only took you 3 tries!

Guess Higher...
Take Another guess: 55
You guessed it!  The number was 85
And it only took you 4 tries!

Guess Higher...
Take Another guess: 33
You guessed it!  The number was 85
And it only took you 5 tries!

Sorry You Lose!!!!
The Number was  85
Guess Higher...
Take Another guess:



More information about the Python-list mailing list