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.")