[Tutor] Help with "Guess the number" script

Ben Finney ben+python at benfinney.id.au
Sun Mar 2 08:43:59 CET 2014


Scott W Dunning <swdunning at cox.net> writes:

> On Mar 1, 2014, at 12:47 AM, Ben Finney <ben+python at benfinney.id.au> wrote:
>
> > You've bound the name ‘current_guess’ to the user's input, but then do
> > nothing with it for the rest of the function; it will be discarded
> > without being used.

> Hmm, I’m not quite sure I understand.  I got somewhat confused because
> the directions were changed a little and current_guess was removed
> from the get_guess function.

Removed by whom? It is still there in the example you posted, and it's
ideal for use.

> Is this more like what I should be doing?
>
> def get_guess(guess_number):
> 	raw_input(“Please enter a guess”)
> 	guess_number = int(guess_number)
> 	return (guess_number)
> get_guess(1)

No, that's the opposite direction :-) Inside the ‘get_guess’ function
you should use as many names as you need for the different purposes.

So, you have one name ‘guess_number’ bound to the function's parameter.
Don't bind anything else to it!

Then, for the return value from ‘raw_input’, you should choose a
different suitable name and bind that name to the value (with an
assignment statement).

Then, for the computed result, you should choose yet another suitable
name, and bind *that* name to the computed value.

All these different names will make clear in the code what the meaning
of each value is.

-- 
 \      “Why should I care about posterity? What's posterity ever done |
  `\                                            for me?” —Groucho Marx |
_o__)                                                                  |
Ben Finney



More information about the Tutor mailing list