[Tutor] Guess my number game

myles broomes mylesbroomes at hotmail.co.uk
Sun Nov 20 16:29:03 CET 2011

I asked for advice yesterday with some pseudocode for a 'guess my number' game I wrote up, and I was told to have a go at writing up the actual code. I've had a go but im struggling...

#guess my number games
#the user thinks of a number between 1 and 100,
#the computer then has to try and guess that number

#welcome the player to the game and explain the rules
print("\t\t\t Guess My Number")
input("Welcome player to the 'Guess My Number' game. Think of a number between 1 and 100 and I will try to guess it. Press enter when you have thought of your number and are ready to begin.")

#import the random module
import random

#set initial values
tries = 1
attempt = random.randint(1,100)
guess = input("Is your number",attempt,"? (Enter 'yes' or 'no'.)")

#guessing loop
while guess == 'no':
	tries += 1
	incorrect = input("Was my guess too high or too low? (Type 'high' or 'low'.) ")
	if incorrect == 'high':
	if incorrect == 'low':
#tell the player how many tries it took and end the game
print("Aha! I guessed it! And it only took",tries,"tries!")
input("Press enter to exit.")
Im having trouble trying to figure out a way to make the program guess 'higher' and 'lower' (Where the two asterisks are). Any help would be much appreciated! Oh and heres my pseudocode:

Welcome the user to the game
Explain the rules of the game
Wait for the user to think of a number
Once the user has thought of their number, take a guess
While the number has not been guessed correctly
	Increase the number of 'tries' by 1
	Ask the user if the guess was too high or too low
	If the guess was too high
		guess lower
	If the guess was too low
		guess higher
Once the number has been guessed correctly, tell the user the number of tries it took
Exit the game

Thanks again!

More information about the Tutor mailing list