Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #76772

Re: Very basic question. How do I start again?

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.python
Subject Re: Very basic question. How do I start again?
Date 2014-08-22 04:26 +0000
Organization A noiseless patient Spider
Message-ID <lt6gp9$tr7$1@dont-email.me> (permalink)
References <1e5dv9l3aik5bb1jn4vil731rdl7q6sl3l@4ax.com>

Show all headers | View raw


On Thu, 21 Aug 2014 21:37:22 -0400, Seymore4Head wrote:

> I want to give the computer 100 tries to guess a random number between 1
> and 100 picked by the computer.
> 
> For the moment I am always using 37 as the random pick.  I want to
> change the pick to pick=random.randrange(1,100).  The program works as
> expected until the computer gets a correct guess.  I don't know what I
> should be doing to restart the program when pick=guess.
> 
> It is supposed to let the computer pick a number between 1 and 100 and
> then let the computer guess the answer.  If the computer picks a low
> number the next guess is supposed to be limited to higher numbers than
> the guess.  If the computer picks a high number, the next guess is
> supposed to be limited to lower numbers than the first guess.
> 
> The program fails when guess=pick
> 
> import random count = 1      #Start the counter at 1 low=1           #
> the low range of 1 to 10 high=100      #The high range of 1 to 100 pick
> = 37      # Will change to pick=random.randrange(1,100)
> guess = 0     #Guess is the computer's guess at pick print ("Time to
> play a guessing game.")
> print ("")
> 
> 
> while count < 100:
>     guess = random.randrange(low,high)
>     print (pick, guess)
>     if guess == pick:
>         print ("correct")
> 
> #"What I need is something here that says start over"
> 
>     elif guess < pick:
>         low=guess+1 print ("Too low")
>     elif guess > pick:
>         high=guess-1 print ("Too high")
>     count = count +1
> 
> (I can see where adding a 25 then 10 increment later would speed up the
> guessing)

Write the problem out in basic english terms, then translate these to the 
program. The english might look like this (laid out in a pythonic manner):

while I want to play a game:
    choose a number
    guess the answer
    tries = 1
    while guess != choice:
        guess another answer
        tries = tries + 1
    print "it took " + tries + " attempts to guess " + choice

This simplification doesn't take the calculation of ranges into account, 
but that's part of "guess the/another answer".

-- 
Denis McMahon, denismfmcmahon@gmail.com

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Very basic question.  How do I start again?   Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-21 21:37 -0400
  Re: Very basic question.  How do I start again? Ben Finney <ben+python@benfinney.id.au> - 2014-08-22 11:55 +1000
    Re: Very basic question.  How do I start again? Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-21 22:13 -0400
      Re: Very basic question. How do I start again? Chris Angelico <rosuav@gmail.com> - 2014-08-22 12:24 +1000
  Re: Very basic question. How do I start again? Chris Angelico <rosuav@gmail.com> - 2014-08-22 11:58 +1000
    Re: Very basic question. How do I start again? Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-08-21 22:18 -0400
      Re: Very basic question. How do I start again? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-08-21 23:17 -0400
  Re: Very basic question.  How do I start again?   Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-22 12:58 +1000
  Re: Very basic question.  How do I start again? Denis McMahon <denismfmcmahon@gmail.com> - 2014-08-22 04:26 +0000
  Re: Very basic question.  How do I start again?   Tim Roberts <timr@probo.com> - 2014-08-21 22:56 -0700
    Re: Very basic question. How do I start again? Igor Korot <ikorot01@gmail.com> - 2014-08-21 23:21 -0700

csiph-web