Mini project — Guess the number in Python
this game is extended later in the projects Refactor the Guess the number game (SR) and The Guess the number game (SR).
In this Python mini project for beginners we will make a simple console game "Guess the number" in Python. Through this example you will learn how to use a while loop, if / else branching, and reading input with the input() function. This task is ideal as a practical Python exercise and one of the most common programming tasks for beginners.
1. A description of the "Guess the number" game
- The computer randomly chooses a number between 1 and 100.
- The user tries to guess the number by typing it on the keyboard.
- The program uses Python branching to say whether the number is smaller or greater.
- The game uses a Python while loop to repeat the tries.
- The game ends when the user guesses the number or uses up all the tries.
2. The basic code of the game
Let us start with the basic version of the game. Later we will improve it.
import random
secret_number = random.randint(1, 100)
guess = None
print("Guess a number between 1 and 100!")
while guess != secret_number:
guess = int(input("Enter your number: "))
if guess < secret_number:
print("The number is greater!")
elif guess > secret_number:
print("The number is smaller!")
else:
print("Congratulations! You guessed the number:", secret_number)
3. An improved version of the game
In real programs we should never assume that the user will always enter correct data. That is why we add input validation, a counter of tries and a limit on the number of tries.
import random
secret_number = random.randint(1, 100)
max_tries = 10
counter = 0
print("Guess a number between 1 and 100!")
print("You have", max_tries, "tries.")
print("Enter 0 if you want to stop the game.")
while counter < max_tries:
try:
guess = int(input("Enter your number: "))
except ValueError:
print("Error! You must enter a whole number.")
continue
if guess == 0:
print("Game stopped. The secret number was:", secret_number)
break
counter += 1
if guess < secret_number:
print("The number is greater!")
elif guess > secret_number:
print("The number is smaller!")
else:
print("Congratulations! You guessed the number in", counter, "tries!")
break
else:
print("Unfortunately, you have used all your tries.")
print("The secret number was:", secret_number)
An example of a game (the secret number is random, so your game will be different):
Guess a number between 1 and 100!
You have 10 tries.
Enter 0 if you want to stop the game.
Enter your number: 50
The number is greater!
Enter your number: abc
Error! You must enter a whole number.
Enter your number: 25
The number is greater!
Enter your number: 0
Game stopped. The secret number was: 614. An explanation of the important parts of the code
- random.randint(1, 100) — chooses a random whole number in the given range.
- try / except — stops the program from crashing if the user enters text.
- counter — keeps track of how many times the user has tried to guess the number.
- while counter < max_tries — limits how long the game lasts.
- break — stops the loop when the game is over.
5. Practice tasks
- Let the user choose the range of numbers (e.g. 1–50 or 1–500).
- Add difficulty levels (easy, medium, hard).
- Save all the entered numbers in a list and show them at the end of the game.
- Add an option for the user to play again without restarting the program.
This mini project is an important step towards making real Python programs. When you understand this game, you will be ready for more complex projects, working with functions and data structures.
Appendix: an extended version of the game
In the previous example you saw the basic version of the game. Now we will improve it a little so that it behaves more realistically and is more resistant to wrong input from the user.
1. Limiting the number of tries
In most games the player does not have an unlimited number of tries. That is why we introduce a variable that sets the maximum number of tries and a counter that keeps track of how many times the user has tried.
max_tries = 10
counter = 0
We use these variables in the while loop so that the game ends automatically when all the tries are used up.
2. Checking that the input is correct
So far we have assumed that the user always enters a whole number. In practice that is often not the case — the user can enter a letter or some symbol.
To prevent an error and the program stopping, we use a try / except block.
try:
guess = int(input("Enter your number: "))
except ValueError:
print("Error! You must enter a whole number.")
continue
If an error happens, the program prints a message and goes back to the next round of the loop instead of crashing.
3. The option to stop the game
It is good to give the user control over the program. In this example we allow stopping the game if the user enters the number 0.
if guess == 0:
print("Game stopped.")
break
The command break stops the while loop and ends the game. This is a simple but very useful mechanism in interactive programs.
4. A message after the game ends
When the game ends (either with a correct guess or when the tries run out), it is important that the user gets clear feedback.
print("The secret number was:", secret_number)
Messages like this make the program more pleasant to use and easier to understand.
In the following lessons we will learn how to improve this code further by using functions and make it clearer and reusable.
Further learning – related Python lessons
If you want to extend your knowledge and better understand the ideas used in this game, we recommend the following lessons that are directly connected with this mini project:
- Python while and for loops – explanation and examples
- Python branching (if / elif / else) (SR)
- Input and output in Python (input and print) (SR)
- Lists in Python – keeping and processing several values
- Python mini projects for beginners (SR)
These lessons will help you understand better how Python programs work and prepare you for the next step — working with functions and more complex projects.
