Very basic question. How do I start again?
Denis McMahon
denismfmcmahon at gmail.com
Fri Aug 22 00:26:17 EDT 2014
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 at gmail.com
More information about the Python-list
mailing list