### Overview

This small program extends the previous guessing game I wrote about in this
: post “Python Guessing Game”.

### Guessing Game

In this game we will add a counter for how many guesses the user can have.

The counter is initial set to zero.

The while loop will run as long as the guesses are less to 5.

If the user guess the right number before that, the script will break and present the user with how many guesses it took to guess the right number.

The variables in this script can be changed to anything.

I will break up the program in chunks to make it easier to read

First we import the Random module

import random

Then we give the “number” variable a random number between 1 and 99.

number = random.randint(1, 99)

Set the guesses variable to 0, which will count the guesses

guesses = 0

As long as the guesses are less than 5, ask the user to guess a number.

Then increase the guesses counter with 1.

Print out a message to the user the number of guesses.

while guesses < 5:
guess = int(raw_input("Enter an integer from 1 to 99: "))
guesses +=1
print "this is your %d guess" %guesses

Check if the guess is lower, higher or equal to our random number and print a message of the result.

If the guess is the same as our number, exit the program.

if guess < number:
print "guess is low"
elif guess > number:
print "guess is high"
elif guess == number:
break

Print out the how many guesses the user had.

if guess == number:
guesses = str(guesses)
print "You guess it in : ", guesses + " guesses"

If the user couldn’t guess the right number in 5 guesses, print out what the secret number was.

if guess != number:
number = str(number)
print "The secret number was",  number

I hope you had fun with this guessing game.