[Tutor] Tutor Digest, Vol 121, Issue 3

shyam kankala shyamk.py at gmail.com
Sun Mar 16 18:42:25 CET 2014


hi,

   Here you have the Answer. Plz go through it....



   1. # This is a guess the number game.
   2. import random
   3.
   4. guessesTaken = 0
   5.
   6. print('Hello! What is your name?')
   7. myName = input()
   8.
   9. number = random.randint(1, 20)
   10. print('Well, ' + myName + ', I am thinking of a number between 1 and
   20.')
   11.
   12. while guessesTaken < 6:
   13.     print('Take a guess.') # There are four spaces in front of print.
   14.     guess = input()
   15.     guess = int(guess)
   16.
   17.     guessesTaken = guessesTaken + 1
   18.
   19.     if guess < number:
   20.         print('Your guess is too low.') # There are eight spaces in
   front of print.
   21.
   22.     if guess > number:
   23.         print('Your guess is too high.')
   24.
   25.     if guess == number:
   26.         break
   27.
   28. if guess == number:
   29.     guessesTaken = str(guessesTaken)
   30.     print('Good job, ' + myName + '! You guessed my number in ' +
   guessesTaken + ' guesses!')
   31.
   32. if guess != number:
   33.     number = str(number)
   34.     print('Nope. The number I was thinking of was ' + number)



On Sun, Mar 2, 2014 at 1:03 PM, <tutor-request at python.org> wrote:

> Send Tutor mailing list submissions to
>         tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
>         tutor-request at python.org
>
> You can reach the person managing the list at
>         tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>    1. Re: Help with "Guess the number" script (spir)
>    2. Re: Help with "Guess the Number" script (Mark Lawrence)
>    3. Re: When to use multiprocessing Managers? (eryksun)
>    4. Re: Help with "Guess the number" script (Alan Gauld)
>    5. Re: Help with "Guess the number" script (Alan Gauld)
>    6. Re: Help with "Guess the number" script (Scott W Dunning)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Sat, 01 Mar 2014 14:53:57 +0100
> From: spir <denis.spir at gmail.com>
> To: tutor at python.org
> Subject: Re: [Tutor] Help with "Guess the number" script
> Message-ID: <5311E675.9020102 at gmail.com>
> Content-Type: text/plain; charset=UTF-8; format=flowed
>
> 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
>
>
>
> ------------------------------
>
> Message: 2
> Date: Sat, 01 Mar 2014 15:57:22 +0000
> From: Mark Lawrence <breamoreboy at yahoo.co.uk>
> To: tutor at python.org
> Subject: Re: [Tutor] Help with "Guess the Number" script
> Message-ID: <lesvvt$hts$1 at ger.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 01/03/2014 06:05, Scott Dunning wrote:
>
> In addition to the answers you've already had, I suggest that you learn
> to run code at the interactive prompt, it's a great way of seeing
> precisely what snippets of code actually do.  Also use the print
> statement in Python 2 or print function in Python 3, again a great way
> to observe what your code is doing.
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
>
> Mark Lawrence
>
> ---
> This email is free from viruses and malware because avast! Antivirus
> protection is active.
> http://www.avast.com
>
>
>
>
> ------------------------------
>
> Message: 3
> Date: Sat, 1 Mar 2014 11:48:25 -0500
> From: eryksun <eryksun at gmail.com>
> To: James Chapman <james at uplinkzero.com>
> Cc: tutor at python.org
> Subject: Re: [Tutor] When to use multiprocessing Managers?
> Message-ID:
>         <CACL+1atqpfeZ9P1n3bQ0HvUJod=
> LMuN9YLYzuphVJmPTQcAeUA at mail.gmail.com>
> Content-Type: text/plain; charset=UTF-8
>
> On Fri, Feb 28, 2014 at 6:31 AM, James Chapman <james at uplinkzero.com>
> wrote:
> >
> > log_Q = multiprocessing.Queue()
>
> This is a Queue from multiprocessing.queues. It uses system resources
> (e.g. a semaphore for the queue capacity) that can be shared with
> processes on the same machine.
>
> A value `put` in a queue.Queue is available immediately:
>
>     >>> import queue
>     >>> q1 = queue.Queue()
>     >>> try: q1.put('value'); q1.get_nowait()
>     ... except queue.Empty: 'empty'
>     ...
>     'value'
>
> On the other hand, a Queue from multiprocessing.queues writes to a
> pipe using a background thread, so there can be a small delay:
>
>     >>> import multiprocessing as mp
>     >>> q2 = mp.Queue()
>     >>> try: q2.put('value'); q2.get_nowait()
>     ... except queue.Empty: 'empty'
>     ...
>     'empty'
>     >>> q2.get_nowait()
>     'value'
>
> > or whether I create it like this:
> >
> > multimanager = multiprocessing.Manager()
> > log_Q = multimanager.Queue()
>
> This is a queue.Queue wrapped by an AutoProxy. For example, its `get`
> method calls _callmethod('get', *args, **kwds), which connects to the
> manager, sends the request, and receives the result.
>
> The docs demonstrate using a manager with remote processes:
>
> http://docs.python.org/3/library/multiprocessing#using-a-remote-manager
>
> You can also proxy the Queue type from multiprocessing.queues. In that
> case, remote processes use a proxy, but local processes can use the
> queue directly.
>
> > Perhaps the manager would be important if I was writing to a Queue and
> > expecting all threads to see that message?
>
> Only 1 thread will `get` the message.
>
>
> ------------------------------
>
> Message: 4
> Date: Sat, 01 Mar 2014 17:16:02 +0000
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] Help with "Guess the number" script
> Message-ID: <let4k4$trb$1 at ger.gmane.org>
> Content-Type: text/plain; charset=UTF-8; format=flowed
>
>
> Scott W Dunning <swdunning at cox.net> writes:
>
> > def get_guess(guess_number):
> >      print "(",guess_number,")""Plese enter a guess:"
>
> Aren't you missing a comma before the last string?
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
>
>
>
> ------------------------------
>
> Message: 5
> Date: Sat, 01 Mar 2014 17:29:46 +0000
> From: Alan Gauld <alan.gauld at btinternet.com>
> To: tutor at python.org
> Subject: Re: [Tutor] Help with "Guess the number" script
> Message-ID: <let5ds$943$1 at ger.gmane.org>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 01/03/14 17:16, Alan Gauld wrote:
> >
> > Scott W Dunning <swdunning at cox.net> writes:
> >
> >> def get_guess(guess_number):
> >>      print "(",guess_number,")""Plese enter a guess:"
> >
> > Aren't you missing a comma before the last string?
> >
> I just realized it will work because Python auto joins adjacent
> string literals. But in this case you should probably just
> remove both quotes and make it more readable.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.flickr.com/photos/alangauldphotos
>
>
>
> ------------------------------
>
> Message: 6
> Date: Sat, 1 Mar 2014 17:43:57 -0700
> From: Scott W Dunning <swdunning at cox.net>
> To: Ben Finney <ben+python at benfinney.id.au>
> Cc: tutor at python.org
> Subject: Re: [Tutor] Help with "Guess the number" script
> Message-ID: <E10BAA23-7B2D-4EB8-978A-66C6C10F86FB at cox.net>
> Content-Type: text/plain; charset=windows-1252
>
>
> 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.  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)
>
> >
> > Then, you use the parameter ?guess_number?, create a new integer from
> > it, and return that integer. I think you've used the wrong name for the
> > ?int()? parameter.
> Well, since there are no loops allowed I?m guessing get_guess will be
> called 9 times.  I believe guess_number is the number of tries the user has
> used.
> So;
> (1) Please enter a guess:
> (2) Please enter a guess:
>
>
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> https://mail.python.org/mailman/listinfo/tutor
>
>
> ------------------------------
>
> End of Tutor Digest, Vol 121, Issue 3
> *************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140316/ebf170bd/attachment-0001.html>


More information about the Tutor mailing list