On 03/03/2014 05:03 AM, Scott W Dunning wrote:

Ben Finney makes numerous fine comments already. I'll add a few, some on the same points but but expressed a bit differently (case it helps).

This is what Im having trouble with now.  Here are the directions I’m stuck on 
and what I have so far, I’ll bold the part that’s dealing with the instructions 
if anyone could help me figure out where I’m going wrong.

Thanks!

from random import randrange
randrange(1, 101)
from random import seed
seed(129)

def print_description():
     print """Welcome to Guess the Number.
     I have seleted a secret number in the range 1 ... 100.
     You must guess the number within 10 tries.
     I will tell you if you ar high or low, and
     I will tell you if you are hot or cold.\n"""

def get_guess(guess_number):
     promt = "(" + str(guess_number) +") Please enter a guess:"
     user_guess = raw_input(promt)
     user_guess = int(user_guess)
     return user_guess

Very good choice of variable name for 'promt'. (Apart from ortography, but since you are consistent with the error... ;-)

There are 2 user guesses here, and only 1 variable, thus 1 name. The name should say what (idea) the variable represents in the program; this should be said by the name's *meaning*. It is one of the greatest difficulties in programming. How would you define what these variables represent, using everyday language? My own definitions would lead me to choose the following variable names:
     guess_text   = raw_input(promt)
     guess_number = int(user_guess)
     return guess_number
Note: it is especially obviuos that these are 2 separate numbers, since they do not even are of the same type (a piece of text, or "string", vs a number, here an "int").

Good naming is very, very hard; differences of naming can make some piece of program nearly trivial or instead nearly impossible to understand; often bad naming is worse than hypothetical randomly chosen names, because bad naming *misleads* your thinking.

Changing the value of a local variable is always, or nearly, a sign that there are here 2 ideas which should be represented by 2 variables with 2 names. Example of 2 programming styles (note the difference in ease of understanding, even if you don't know the python features used here):

def get_data (data):
    data = File(data)           # (a file)
    data = data.read()          # (a piece of text)
    data = data.split("")     # (a list of words)
    return data
...
data = get_data("data.text")

def data_words (file_name):
    data_file = File(file_name)         # (a file)
    text = data_file.read()             # (a piece of text)
    words = text.split(" ")           # (a list of words)
    return words
...
words = data_words("data.text")

(A special case is loop variables, but even then you only write the assignment once, the value chages across multiple passes on the same code. The only real exception is accumulators, like for computing a sum, which need to be first initialised to a start value, often 0.)

def print_hints(secrets, guess):
     secret_number = secret
     guess = guess
     if guess < 0 or user_guess> 101:
         print "Out of range!"

Parameters are input variables. Once they are given execution values by a call 
like
    print_hints(input_value1, input_value2)

these variables exist _inside_ the function body (each with a name and a value). As if functions were defined like:
    def print_hints:            # note: no param
        secret = input_value1
        guess  = input_value2
        ... use these variables ...
This is more or less what the language does for you. This is the whole point of defining parameters, in fact. So, _you_ do not need to _rebind_ parameters to local variables; they already are local variables.

In addition, you are not consistent with variable _names_, evendently, so your programs have no chance to work. This is an annoying, but necessary part of programming. But the language will always tell about such errors, at once, *if and only if* the wrong name does *not* otherwise exist. --> pay attention!

def main():
     print_description()
     secret = randrange(1,101)
     current_guess = get_guess(1)
     if current_guess != secret:
         print_hints(secret_number, guess)
         current_guess = get_guess(2)

* 'secret_number' appears from nowhere: pay attention!
* To be more coherent checking if the guess is right or wrong (or too high or too low) should be done in function print_hints as well. This function _evaluates_ the guess (maybe it should be renamed).

     if secret == current_guess:
         print "Congratulations, you win!"
     else:
         print "Please play again"
     print "The secret number was", secret

These are (also) hints to the player, actually, aren't they?

main()

d
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to