What is a number guessing game?
A number guessing game is a small program where the computer chooses a secret number and the player tries to guess it. It is a great beginner project because it combines several core programming concepts in one simple example.
In this tutorial, the game will choose a random number between 1 and 6. The user will enter a guess, and Python will check whether the guess is correct.
Python concepts used in this project
This mini project is simple, but it teaches useful Python fundamentals:
- Importing a module with import
- Generating random numbers with random.randint()
- Getting user input with input()
- Converting text input to an integer using int()
- Using if and else conditions
The random module
Python has a built-in module called random. This module can generate random values. For this game, we use random.randint(1, 6) to generate a random integer from 1 to 6.
The function random.randint(1, 6) includes both 1 and 6 in the possible results.
Full Python code
Here is the complete beginner version of the game. You can copy it and run it in your Python editor or terminal.
import random
secret_number = random.randint(1, 6)
guess = int(input("Guess a number between 1 and 6: "))
if guess == secret_number:
print("Correct! You guessed the number.")
else:
print("Wrong guess.")
print("The correct number was:", secret_number)
How the code works
1. Import the random module
The first line imports Python's built-in random module. Without this import, Python would not recognize random.randint().
2. Generate the secret number
The variable secret_number stores a random number from 1 to 6. This is the number the player needs to guess.
3. Ask the user for a guess
The input() function receives the user's guess as text. Because we want to compare numbers, we convert the input into an integer using int().
4. Compare the guess
The condition guess == secret_number checks whether the player's guess is the same as the secret number. If it is, the player wins. Otherwise, Python prints the correct answer.
Example output
If the secret number is 4 and the user guesses 4, the output may look like this:
Guess a number between 1 and 6: 4
Correct! You guessed the number.
If the secret number is 2 and the user guesses 5, the output may look like this:
Guess a number between 1 and 6: 5
Wrong guess.
The correct number was: 2
Practice challenge
Once the basic version works, try improving the game. These changes will help you practice loops, validation, and better game logic.
- Give the player 3 chances instead of 1.
- Check whether the user's input is between 1 and 6.
- Add a score system.
- Ask the player if they want to play again.
Improved idea: three attempts
A better version of this game could use a loop so the player gets multiple attempts. This is a good next step after understanding the basic version.
import random
secret_number = random.randint(1, 6)
for attempt in range(1, 4):
guess = int(input("Guess a number between 1 and 6: "))
if guess == secret_number:
print("Correct! You guessed the number.")
break
else:
print("Wrong guess.")
print("The correct number was:", secret_number)
Related pages
Explore more Python practice examples and services through the links below.