Very basic question. How do I start again?

Seymore4Head Seymore4Head at Hotmail.invalid
Thu Aug 21 21:37:22 EDT 2014


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)



More information about the Python-list mailing list