[Tutor] Help with "Guess the number" script

spir denis.spir at gmail.com
Sat Mar 1 14:53:57 CET 2014


On 03/01/2014 07:46 AM, Scott W Dunning wrote:
> Hello, i am working on a project for learning python and I’m stuck.  The directions are confusing me.  Please keep in mind I’m very ne to this.  The directions are long so I’ll just add the paragraphs I’m confused about and my code if someone could help me out I’d greatly appreciate it!  Also, we haven’t learned loops yet so just conditional operators and for some reason we can’t use global variables.
>
>
> 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):
>      print "(",guess_number,")""Plese enter a guess:"
>      current_guess = raw_input()
>      return int(guess_number)
>
> def main():
>      print_description()
>      secret = 50
>      current_guess = 1
>      get_guess(1)
>      if current_guess != secret():
>          print "Congratulations you win!!"
>
> main()
>
>
> Here are the instructions I’m having a hard time with and just not sure I’m doing it correctly.  I’m not sure the get_guess function is correct and I’m a little lost with the secret and current_guess variable.
>
>  From within the body of the main function, immediately after the call to print description, create variable secret and assign it a random number between 1 and 100, generated using the randrange function. You will need to pass two argument to randrange, what do you think they should be? You should be able to use the python help system or online python documentation to make sure you understand the arguments to randrange.
>
> After the end of the body of the print description function, define a new global function named get guess that takes a single parameter. Name the parameter guess number, because it will hold the index (1, 2, 3, ..., 10) of current guess attempt. Make the function ask the user to enter guess using the raw input function. The function will return the number entered by the user, after it has been converted to an integer.
>
> Return to the main function after the statement that assigned a value to the secret variable. In a new variable named current guess store the result of calling the get guess function with an argument of 1. Run your program to make sure it works correctly.
>
> At the end of the main function, check if the current guess matches the secret. If it matches, print ‘Congratulations, you win!’. If it does not, print ‘Please play again!’

I find directions very confusing. Also, they completely control you while 
explaining about nothing, like a user manual saying "press this, turn that". 
This is inappropriate for programming (and anything else): you need to 
understand! You need the why's and the how's, not only the what's.

If not enough, they seem to teach you pretty weird practices: what is the point 
of the parameter guess_number? It is not a parameter, less so of this function, 
but a counter proper to the game control, possibly used at the end to write "You 
won in [counter] trials." But it is not and cannot be used as a parameter to 
get_guess. Also, what is the point of requiring you to write this game without a 
loop? You need a loop. If they want to teach you other notions first, they must 
find another sample program.

If the rest of the book is similar, I would encourage you to change. Maybe try 
one of those (or why not both in //):

* Alan Gauld's "Learning to Program": a very good point is this guide teaches to 
program, in general, *using* Python, mainly:
   http://www.alan-g.me.uk/l2p/index.htm

* Al sweigart's "invent with python": this one teaches python & programming, 
using games as learning material:
   http://inventwithpython.com/

The first example in the latter book is precisely "guess my number". So, you can 
find correct code for it there.

d



More information about the Tutor mailing list