[Tutor] Help with "Guess the number" script
Alan Gauld
alan.gauld at btinternet.com
Tue Mar 4 11:45:31 CET 2014
On 04/03/14 02:29, Scott W Dunning wrote:
> I’ve made some changes and have a couple questions, I’ll speak in
> between the code.
> from random import randrange
> randrange(1, 101)
This call to randrange() doesn't do anything because you
don't store the return value. You need to create a variable
to hold the random number.
> from random import seed
You can import several names at once:
from random import randrange, seed
> seed(129)
While this works for your purposes it is going to give
you the same 'random' numbers each time. That is they
won't really be random. The normal way to seed a random
number is to use a number that changes each time,
such as the current time. But you can leave that as
a nicety for later...
> So, this is what I gather from the function below. I’m basically taking
> the parameter guess_number and making it a str with prompt, then using
> the variable guess_text and turning the prompt into an int? and
> returning the original value the user inputed?
You have the gist of it but you need to watch your terminology.
prompt is just the variable, its only a name or label that
you attach to your data. It doesn't do anything. So your
comment above should say:
"I’m basically taking the parameter guess_number and making
it a str and storing the string as the variable prompt"
The next comment isn't quite right either. The raw_input() function
displays the prompt to the user and returns the value that the user
entered as a string. You then convert that string to an integer and
store (then return) the result. So your comment should say something like:
"then using the variable guess_text to store the value the user had
input and then turning it into an int. Finally returning the converted
value?"
The variable names prompt, guess_text and guess_number
are just temporary storage areas they don't do anything.
The doing is all in the functions: raw_input() and int().
> def get_guess(guess_number):
> prompt = "(" + str(guess_number) +") Please enter a guess:"
> guess_text = raw_input(prompt)
> guess_number = int(guess_text)
> return guess_number
> I’m a little confused about the parameter guess below and how I’m able
> to use it with the conditional statement for when it’s out of range? Is
> it because when I call the function print_hints(secret, current_guess)
> I’m essentially defining it because guess_number was turned into
> current_guess when I call it equal to get_guess(1)??
The names in the function (including the parameters) are completely
separate from the names outside it. So you can modify a variable called
guess in your main program and it has no effect on the parameter called
guess. They are completely separate. Its just like people. I have a
cousin called Scott. He is currently climbing mountains in Europe. That
does not mean that you are climbing mountains, you are two different
Scott's. You each have your own context, or as we say in programming,
your own 'namespace'.
The parameters of a function take on the values that are passed to the
function when you call it - the arguments. You may choose to assign the
variable guess to the parameter guess when you call it, or you may
assign a different argument, Python doesn't care. It just takes whatever
argument value is assigned to the guess parameter and uses it within the
function.
> I feel like it would be a lot clearer if we were allowed to use global
> variables.
Trust me, you don't want to do that. It creates a morass of
unmaintainable code and a huge memory test as you try to remember
which functions modify which variables and when. Global variables
are one of the biggest causes of bugs in programs, especially once
you get into bigger projects.
> def print_hints(secret, guess):
> if guess < 1 or guess > 101:
> print
> print "Out of range!"
> print
There is a small bug in this code.
The test should be whether the value is between 1 and 100 but
you are allowing a guess of 101 to pass. I'll leave you to
figure out how to fix that! :-)
> def main():
> print_description()
> secret = randrange(1,101)
> current_guess = get_guess(1)
> if current_guess != secret:
> print "please play again!"
> print_hints(secret, current_guess)
> current_guess = get_guess(2)
I think you are supposed to call print_hints() after setting
current_guess so the order would be something like:
print "please play again!"
current_guess = get_guess(2)
print_hints(secret, current_guess)
Also since you know about functions you could simplify
the code and save typing by putting those three lines
into a another function which returns the guess for
use in the comparison.
Also since this is a function you could reverse
the logic of the comparison and return (thus
exiting the program) when the guess == secret.
That would look something like:
current_guess = your_new_function(1)
if current_guess === secret:
print 'well done'
return
current_guess = your_new_function(2)
if current_guess === secret:
print 'well done'
return
etc...
repeating that however many guesses you want to allow.
In fact you could even wrap that whole thing,
including the test, in a function and instead
of returning the guess return a true/false value
indicating if the guess was successful.
That would then look like:
if bigger_new_function(1):
return
if bigger_new_function(2):
return
etc...
> ....
> if current_guess != secret:
> print "please play again!"
> print_hints(secret, current_guess)
> current_guess = get_guess(10)
> if current_guess == secret:
> print "Congratulations, you win!"
This means you only tell them about success after
comparing the result to secret many times. By inverting
the test as suggested above you can respond to the
correct guess immediately.
> if secret != current_guess:
> print "Please play again"
> print "The secret number was:", secret
This seems a tad pointless, telling them to play again then
giving them the answer, and then the program terminates
before they can try again!
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list