We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,151 Members — Technology Publication meets Social Media

# Guess my number computer version

Hi there one and all. I am attempting to learn to code in python with the intention of teaching it to a class of 12 year olds later in the year!!! So far I've been going ok if a little slowly, however this one is stumping me. I am trying to get the 'Computer' to guess my number as a variation of the standard 'Guess my number game' and just cannot get it to work.

I wonder if someone could run an expert eye over my code below and give me any pointers on how to get it working as I am starting to go a little mad. Thank you in advance.

Pawel.

``````# CPU Guess My Number
#
# You pick a random number between 1 and 100
# The computer tries to guess it in the fewest attempts
# automatically saying if the number is too high or low
# or right on the money
#
# Pawel Aftanas 26/04/12

import random

print "\tWelcome to 'CPU Guess My Number'!"
print "\nI will think of a number between 1 and 100."
print "The CPU will try and guess the number in the fewest attempts possible.\n"

# Human to enter a number for the CPU to guess

the_number = int(raw_input("Enter a number between 1 and 100: "))

# Computer guesses the number using the random function

guess = random.randrange(1,101)
tries = 1

while True:
guess != the_number:
tries += 1
if guess > the_number:
print "You chose", guess, "but my number is lower..."
guess = random.randint(1, guess+1)
elif guess < the_number:
print "You chose", guess, "but my number is higher..."
guess = random.randint(guess-1 ,101)
else:
print "\n\n\t\t\tThat's Right!"
print "\t\t\tthe number was", the_number,"..."
print "\t\t...and it only took", tries,"tries!\n"
break

# Count down sequence

if tries == 8:
print "You have had 8 goes only 2 left!"
elif tries == 9:
print "Only one more chance..."
else:
print "Bad luck after 10 attempts you still haven't guessed my number."
break

raw_input ("\n\nPress enter key to exit.")
``````
3
Contributors
4
Replies
2 Hours
Discussion Span
1 Year Ago
Last Updated
5
Views
pawel1
Newbie Poster
2 posts since Apr 2012
Reputation Points: 0
Skill Endorsements: 0

Usual way to guess optimally is to guess average of low guess (initially 0) and high guess (initially 101).

pyTony
pyMod
Moderator
6,308 posts since Apr 2010
Reputation Points: 879
Skill Endorsements: 26

Right I have started to get somewhere, but still not quite right, the code is not allowing the computer to narrow its choices down any help would be appreciated. Thanks again.

``````# CPU Guess My Number
#
# You pick a random number between 1 and 100
# The computer tries to guess it in the fewest attempts
# automatically saying if the number is too high or low
# or right on the money
#
# Pawel Aftanas 26/04/12

import random

print "\tWelcome to 'CPU Guess My Number'!"
print "\nI will think of a number between 1 and 100."
print "The CPU will try and guess the number in the fewest attempts possible.\n"

# Human to enter a number for the CPU to guess

the_number = int(raw_input("Enter a number between 1 and 100: "))

# Computer guesses the number using the random function

guess = random.randrange(1,101)
tries = 0

# The code is working but it is not narrowing down the choices.
while True:
guess != the_number
tries += 1
if guess > the_number:
print "You chose", guess, "but my number is lower..."
guess = random.randint(1, guess + 1)
elif guess < the_number:
print "You chose", guess, "but my number is higher..."
guess = random.randint(guess - 1 ,101)
else:
print "\n\n\t\t\tThat's Right!"
print "\t\t\tthe number was", the_number,"..."
print "\t\t...and it only took", tries,"tries!\n"
break

# Count down sequence

if tries == 10:
print "Bad luck after 10 attempts you still haven't guessed my number."
break

raw_input ("\n\nPress enter key to exit.")
``````
pawel1
Newbie Poster
2 posts since Apr 2012
Reputation Points: 0
Skill Endorsements: 0

Here is a working version with a varying 'guessrange' :)

``````# CPU Guess My Number
#
# You pick a random number between 1 and 100
# The computer tries to guess it in the fewest attempts
# automatically saying if the number is too high or low
# or right on the money
#
# Pawel Aftanas 26/04/12

import random

print "\tWelcome to 'CPU Guess My Number'!"
print "\nI will think of a number between 1 and 100."
print "The CPU will try and guess the number in the fewest attempts possible.\n"

# Human to enter a number for the CPU to guess

the_number = int(raw_input("Enter a number between 1 and 100: "))

# Computer guesses the number using the random function

guessrange = (1, 101)

tries = 0

# The code is working but it is not narrowing down the choices.
while True:
guess = random.randrange(*guessrange)
tries += 1
if guess > the_number:
print "You chose", guess, "but my number is lower..."
guessrange = (guessrange[0], guess)
elif guess < the_number:
print "You chose", guess, "but my number is higher..."
guessrange = (guess + 1, guessrange[1])
else:
print "\n\n\t\t\tThat's Right!"
print "\t\t\tthe number was", the_number,"..."
print "\t\t...and it only took", tries,"tries!\n"
break

# Count down sequence

if tries == 10:
print "Bad luck after 10 attempts you still haven't guessed my number."
break

raw_input ("\n\nPress enter key to exit.")
``````
Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Skill Endorsements: 11

line 29 of the code does nothing, here for comparision systematic one with average

``````# CPU Guess My Number
#
# You pick a random number between 1 and 100
# The computer tries to guess it in the fewest attempts
# automatically saying if the number is too high or low
# or right on the money
#
# Pawel Aftanas 26/04/12
print "\tWelcome to 'CPU Guess My Number'!"
print "\nI will think of a number between 1 and 100."
print "The CPU will try and guess the number in the fewest attempts possible.\n"

# Human to enter a number for the CPU to guess
the_number = int(raw_input("Enter a number between 1 and 100: "))

# Computer guesses the number using the random function
guessrange = (0, 101)

tries = 0

# The code is working but it is not narrowing down the choices.
while True:
guess = sum(guessrange)/2
tries += 1

if guess > the_number:
print "You chose", guess, "but my number is lower..."
guessrange = (guessrange[0], guess)
elif guess < the_number:
print "You chose", guess, "but my number is higher..."
guessrange = (guess, guessrange[1])
else:
print "\n\n\t\t\tThat's Right!"
print "\t\t\tthe number was", the_number,"..."
print "\t\t...and it only took", tries,"tries!\n"
break

# Count down sequence
if tries == 10:
print "Bad luck after 10 attempts you still haven't guessed my number."
break

raw_input ("\n\nPress enter key to exit.")
``````
pyTony
pyMod
Moderator
6,308 posts since Apr 2010
Reputation Points: 879