[Tutor] Number guessing game
rabidpoobear at gmail.com
Tue Oct 3 07:02:57 CEST 2006
Christopher Hatherly wrote:
> I'm brand new to Python, and almost brand new to programming. I'm
> teaching myself slowly through a 'Python Programming for the Absolute
> Beginner' book, which is great so far.
Awesome, I'm always happy to hear of budding pythonistas :D
> One of the challenge problems
> though, was to write a script for a number guessing game, where you pick
> a number and the computer tries to guess. It took me a couple of days to
> get this working, and some of it is pretty messy. In particular, the
> 'Guess = ...' line, and the
> responses list' that I used. I'm happy that this works, but would like
> to know how to do it better! If anyone could suggest any improvements,
> that'd be great!
As you'll see below, I didn't look through your code to comment on
I thought that if you saw the code I wrote you might get some new ideas
which you could carry over and improve your own code, rather than just
direct 'change this line to this'-type stuff.
If this doesn't help you at all and you want me to go through your code
and comment on specific
points you could improve it, I'll certainly do so, I just thought this
might be more helpful.
Now, I'll take you through the thinking process I'd go through to make
First: What is it we want the program to do?
1. Greet the user.
2. Ask the user for a number
3. Generate a number.
4. Compare this generated number with the user's number.
5. If the generated number is the same as the user's number, goto 7.
6. Otherwise, goto 4. repeat up to (max guesses) times, then goto 7.
7. Tell the user if we managed to guess the number correctly.
8. Say goodbye to the user.
Okay, let's see what variables we'll need for this.
1. The greeting string.
2. The user's number.
3. The generated number.
4. How many guesses we're allowed to make.
5. Whether the guessing was successful.
6. We'll also include the range to guess the numbers,
just to add an extra feature, so we'll need min and max variables.
7. A goodbye string.
We'll also need to use the random module to generate our guesses,
so we don't always guess the same number.
#license = Public Domain
#original author: Luke Paireepinart
#we'll need the random module to make our guesses
#set up these variables we decided were necessary.
#initializing variables like this is helpful because
#it helps people who're reading your code understand what your
#variables are, for the most part, and what you'll probably use them for.
#so make sure they all have nice descriptive names.
greeting = """ Welcome to the number-guessing game.
Hope you enjoy this! Hit enter to continue. """
goodbye = "Thanks for playing the number-guessing game!"
usernum = 0
guessednum = 0
maxguesses = 5
guessed = False
min_guess = 0
max_guess = 100
#First order of business: greeting the user.
#now we want to get a number from him,
#and we'll store it in our usernum variable.
#we're going to assume the nice-user scenario where they know
#that by 'number' you mean 'integer' and not 'random gibberish.'
#you could use a try: -> except: block here, if you couldn't trust
usernum = int(raw_input("What is your number? "))
#once we have this number, we want to start up a loop
#that will continue to guess until it either gets the number right
#or runs out of guesses.
x = 0 #x is an arbitrary loop variable.
while x < maxguesses: #we'll loop until we reach the maximum guess limit.
#we're using random.randrange because it behaves like range(), as
John pointed out earlier.
#this means if you call 'random.randrange(0,100)' the number will be
0 or 99 or anywhere
#in between, but not 100. change the following line to either
#if you want to include both endpoints.
guessednum = random.randrange(min_guess,max_guess)
#now that we have the guessed number, let's compare it to the user's
if usernum == guessednum: #if we guessed right :)
guessed = True
break#let's get out of the loop since we're sure we guessed
elif usernum < guessednum: #we guessed too high.
#the following line has what's called a 'string substitution.'
#all of the '%s' items are replaced by the items in the
parenthesis to the left of the string,
#after the %.
print "We guessed %s instead of %s, which is %s bigger than your
number." % (guessednum,usernum,guessednum-usernum)
elif usernum > guessednum: #we guessed too low.
print "We guessed %s instead of %s, which is %s less than your
number." % (guessednum,usernum,usernum-guessednum)
#and finally, increment our loop-variable so we can keep track of
how many times through
x += 1
#now that the loop has ended, there are two outcomes:
#a. we guessed correctly, and b. we guessed incorrectly.
if guessed: #option a.
#we'll tell them we got it right.
print "We guessed your number, %s!" % usernum
else: #option b.
#we'll tell them they win.
print "We couldn't guess %s, you win!" % usernum
#the game's over, so we'll say goodbye.
#---------------------------End of File.
I hope this helps you in some way.
Good Luck on your learnin'!
More information about the Tutor