[Tutor] Guess the number

Ikaia Leleiwi ileleiwi at gmail.com
Thu May 7 06:09:50 CEST 2015


I am having trouble writing a program that guesses a number inputted by the
user.  A number between 1-100 is inputted into the program and the computer
produces a random integer that is the "guess" as to what the inputted
number might be.  If the guess is lower then the inputted number then the
user inputs the word 'higher' to indicate the computer needs to guess a
higher number.  If the guess is higher than the inputted number then the
user inputs the word 'lower' to indicate the computer needs to guess a
lower number.

My goal is to have this process repeat, continually narrowing down the
range of possible numbers that the computer can guess, until it guesses the
correct number.

The code I have thus far is as follows:

#Computer Guesses Number Game
#The player chooses a number between 1 and 100
#The computer guesses a number
#The player inputs either higher or lower depending whether
#the computer guesses higher than the chosen number or lower
#When the computer guesses correctly it is congratulated

import random

number = int(input("Pick an integer between 1-100 "))

guess = random.randint(1,100)

while guess != number:

    if guess < number:
        print("The computer guesses: ",guess)
        input("higher or lower ")
        guess = random.randint(guess,100)

    elif guess > number:
        print("The computer guesses: ",guess)
        input("higher or lower ")
        guess = random.randint(1,guess)

    else:
        print("Congradulations Computer!! You guessed that the number was
",number)

-----------------------------------

I can't figure out how to narrow down the random integer range as the
computer guesses closer and closer to the actual value of the number chosen
in the beginning.

Any help would be greatly appreciated

Thanks,
Kai


More information about the Tutor mailing list