Discussion:
[Tutor] Guess the number
Ikaia Leleiwi
2015-05-07 04:09:50 UTC
Permalink
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
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-05-07 08:39:44 UTC
Permalink
Post by Ikaia Leleiwi
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.
Note that your code does not do that. It ignores the
'higher' or 'lower' input from the user.
Post by Ikaia Leleiwi
number = int(input("Pick an integer between 1-100 "))
guess = random.randint(1,100)
This if condition should be controlled by the user. So before you get
here you need to print the guess and ask for input.
The if condition then becomes

if next_guess.lower() == 'higher':

or similar
Post by Ikaia Leleiwi
print("The computer guesses: ",guess)
input("higher or lower ")
guess = random.randint(guess,100)
same here, the test should be
Post by Ikaia Leleiwi
print("The computer guesses: ",guess)
input("higher or lower ")
guess = random.randint(1,guess)
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.
You could try storing the max and min values of guess so that the limits
become

randint(guess,hiGuess)

and

randint(loGuess,guess)

And set hiGuess and loGuess when you get an instruction to
go up or down
loGuess = 1
hiGuess = 100
guess = random(loGuess,hiGuess)
number = ...
while:
....
if next_guess == 'higher':
if guess > loGuess: loGuess = guess
elif next_guess == 'lower':
if guess < hiGuess: hiGuess = guess
else:...
guess = randint(loGuess,hiGuess)

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Kewal Patel
2015-05-11 02:50:26 UTC
Permalink
i don't know if this is efficient but i think it works just fine....


import random

# input a number from user

input_number = input("Enter a number")

#function defination

def guess_number(start,stop):
global input_number
try:
g_number = random.randrange(start,stop)
if g_number == input_number :
print "The Input Number is : ",g_number
else:
print "guessed number is :",g_number
reply = raw_input("Enter higher or lower")
if reply == "lower":
guess_number(start,g_number)
else:
guess_number(g_number,stop)
except ValueError,(ex):
print "you have entered wrong answer."

guess_number(1,100)
Post by Ikaia Leleiwi
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.
#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)
print("The computer guesses: ",guess)
input("higher or lower ")
guess = random.randint(guess,100)
print("The computer guesses: ",guess)
input("higher or lower ")
guess = random.randint(1,guess)
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
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Continue reading on narkive:
Loading...