# Python 3 : Guess the number

Now let’s write a script which implements a basic “Guess the number” game. The rules of the game are:

• The user selects if he/she wants to guess a number between 1 and 100 or 1 and 1000
• Based on the number-range the user has a fixed number of guesses
• The application generates a number to guess
• The user enters a number
• if it is the secret number the application congratulates the user and ask if he/she wants to play another round or not
• else the application tells the user if the secret number is less or greater than the number provided
• the guess count is increased
• if the user used up all guesses then the application tells him/her the secret number and asks if the user wants to play another round or not

As you can see, the rules are simple but seem complicated to implement. It is up to you how you want to handle wrong input types (no numbers).

Well, it is not as difficult as it sounds. Let’s see some example output of my solution:

```Should the secret number between 1 and 100 or 1 and 1000? 100
You have chosen 100, you will have 7 guesses to find the secret number.
I have chosen the secret number...
The secret number is higher...
The secret number is higher...
Congrats, you have Won!
The secret number was 66
Do you want to play another round? (yes / no) yes
Should the secret number between 1 and 100 or 1 and 1000? 1000
You have chosen 1000, you will have 10 guesses to find the secret number.
I have chosen the secret number...
The secret number is lower...
The secret number is lower...
The secret number is higher...
The secret number is higher...
The secret number is lower...
The secret number is lower...
The secret number is higher...
The secret number is lower...
The secret number is lower...
The secret number is lower...
Sorry, you lose.
The secret number was 362
Do you want to play another round? (yes / no) no```

As you can see the application does not stop when the guesses run out and it displays a message if the user won or lost.

Let’s look at the code.

```__author__ = 'GHajba'

import random

while True:
while True:
try:
max_number = int(input('Should the secret number between 1 and 100 or 1 and 1000? '))
except ValueError:
print("This was not a number!")
continue
if max_number != 100 and max_number != 1000:
continue
else:
break

if max_number == 100:
guess_count = 7
else:
guess_count = 10

print('You have chosen {}, you will have {} guesses to find the secret number.'.format(max_number, guess_count))

secret_number = random.randint(1, max_number)

print('I have chosen the secret number...')

guesses = 0

while guess_count - guesses:
try:
guesses += 1
guessed = int(input("What's your guess? "))
except ValueError:
continue
if guessed == secret_number:
print('Congrats, you have Won!')
break
elif guessed > secret_number:
print('The secret number is lower...')
else:
print('The secret number is higher...')
else:
print("Sorry, you lose.")

print("The secret number was ", secret_number)
while answer.lower() not in ['yes', 'no', 'y', 'n']:
answer = input("Do you want to play another round? (yes / no) ")

`for guesses in range(guess_count):`