[Tutor] Help with "Guess the number" script
Scott W Dunning
swdunning at cox.net
Sat Mar 1 07:46:02 CET 2014
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!’
Thanks again!!!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140228/0726ae95/attachment-0001.html>
More information about the Tutor
mailing list