[Tutor] Help with "Guess the number" script

Scott W Dunning swdunning at cox.net
Tue Mar 4 03:29:26 CET 2014


On Mar 3, 2014, at 3:27 AM, spir <denis.spir at gmail.com> wrote:
> 
> 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?

***Wow, thanks a lot Denis!!  Although I didn’t get everything you said I definitely understand a lot more!****

I’ve made some changes and have a couple questions, I’ll speak in between the code.  Thanks again everyone, especially for your patients, I know I’ve made several mistakes and I don’t know anything about programming and even less about these forums.  I apologize about that and again appreciate your patients!!!


from random import randrange
randrange(1, 101)
from random import seed
seed(129)
def print_description():
    print """Welcome to Guess the Number.
    I have selected a secret number in the range 1 ... 100.
    You must guess the number within 10 tries.
    I will tell you if you are high or low, and
    I will tell you if you are hot or cold.\n"""
   
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?  

	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)??  I hope I’m not just confusing you guys.  I’m just trying to make sense of everything.  I feel like it would be a lot clearer if we were allowed to use global variables.  Anyways…

	def print_hints(secret, guess):
    		if guess < 1 or guess > 101:
       			print
        		print "Out of range!"
       			print


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)
    if current_guess != secret:
        print "please play again!"
        print_hints(secret, current_guess)
        current_guess = get_guess(3)
    if current_guess != secret:
        print "please play again!"
        print_hints(secret, current_guess)
        current_guess = get_guess(4)
    if current_guess != secret:
        print "please play again!"
        print_hints(secret, current_guess)
        current_guess = get_guess(5)
    if current_guess != secret:
        print "please play again!"
        print_hints(secret, current_guess)
        current_guess = get_guess(6)
    if current_guess != secret:
        print "please play again!"
        print_hints(secret, current_guess)
        current_guess = get_guess(7)
    if current_guess != secret:
        print "please play again!"
        print_hints(secret, current_guess)
        current_guess = get_guess(8)
    if current_guess != secret:
        print "please play again!"
        print_hints(secret, current_guess)
        current_guess = get_guess(9)
    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!"
    if secret != current_guess:
        print "Please play again"
    print "The secret number was:", secret

main()

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140303/fb9e5112/attachment.html>


More information about the Tutor mailing list