Want to improve your Python skills while having fun? This beginner-friendly project will guide you through building a Number Guessing Game using Python, step by step.
By the end, you will have a working python script where the computer picks a random target number, and the player must guess the correct number with higher or lower hints along the way.
This project is perfect for beginners because it covers core programming concepts:
- Using the
randommodule to generate random numbers - Working with loops and conditional statements
- Handling user input, validating errors, and checking a specified range
- Tracking a best score (including an optional saved high score for future runs)
- Understanding indentation and how it controls code blocks in Python
Let’s dive in.
Step 1: Setting Up the Project
Before we start coding, let’s set up our Python project:
- Make sure Python is installed on your computer. If not, download it from the official Python website.
- Open your favorite code editor or IDE.
- Create a new Python file, for example,
number_guess.py.
Great, now, let's dive head first into our Python editor to get this build started.
Step 2: Understanding How the Game Works
The Number Guessing Game works like this:
- The computer picks a random number in a specified range, for example, 1 to 10.
- The player keeps guessing until they get the correct number.
- After each incorrect guess, the player gets a hint, higher or lower.
- The program keeps track of the number of attempts.
- The player’s best score, the fewest attempts, can be saved. If there is none yet, the game starts with none as the best score.
Step 3: Importing the Required Modules
We need the Python random module to generate random numbers. We will also use json and os to optionally save the best score between runs.
import random
import json
import os
import sysThe random.randint(a, b) function generates a random number between a and b (inclusive).
Step 4: Displaying (and Saving) the Best Score
Since we want to track the player’s best score, we will store the lowest number of attempts. To make the “high score” real, we will also save it to a small JSON file, so it carries over to the next time you run the python script.
SCORE_FILE = "best_score.json"
def load_best_score():
if not os.path.exists(SCORE_FILE):
return None
try:
with open(SCORE_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
best = data.get("best_attempts")
if isinstance(best, int) and best > 0:
return best
return None
except (OSError, json.JSONDecodeError):
return None
def save_best_score(best_attempts):
try:
with open(SCORE_FILE, "w", encoding="utf-8") as f:
json.dump({"best_attempts": best_attempts}, f)
except OSError:
pass
def show_score(best_attempts):
if best_attempts is None:
print("There is currently no best score, it's yours for the taking!")
else:
print(f"The current best score is {best_attempts} attempts.")How it works
- If there is no saved score yet,
load_best_score()returnsNone. - If a score exists, we display the best score, the lowest attempt count.
- After a win, we update and save the best score only if the new score is better.
Step 5: Writing the Main Game Logic
Now, let’s write the function that runs the game. Notice how indentation controls what runs inside the loop and inside the conditional blocks. If indentation is off, Python will not run the way you expect.
MIN_NUM = 1
MAX_NUM = 10
def start_game():
best_attempts = load_best_score()
print("Hello, traveler! Welcome to the game of guesses!")
player_name = input("What is your name? ").strip() or "Player"
wanna_play = input(f"Hi, {player_name}, would you like to play the guessing game? (Enter Yes/No): ").strip().lower()
if wanna_play != "yes":
print("That's cool, thanks!")
sys.exit(0)
show_score(best_attempts)
while wanna_play == "yes":
attempts = 0
target_number = random.randint(MIN_NUM, MAX_NUM)
while True:
raw = input(f"Pick a number between {MIN_NUM} and {MAX_NUM} (or type q to quit): ").strip().lower()
if raw in ("q", "quit"):
print("That's cool, have a good one!")
sys.exit(0)
try:
user_guess = int(raw)
if user_guess < MIN_NUM or user_guess > MAX_NUM:
raise ValueError(f"Please guess a number within the specified range ({MIN_NUM} to {MAX_NUM}).")
except ValueError as err:
print("Oh no! That is not a valid number. Try again...")
print(err)
continue
attempts += 1
if user_guess == target_number:
print("Nice! You got it!")
print(f"It took you {attempts} attempts.")
if best_attempts is None or attempts < best_attempts:
best_attempts = attempts
save_best_score(best_attempts)
print("New best score saved.")
wanna_play = input("Would you like to play again? (Enter Yes/No): ").strip().lower()
if wanna_play != "yes":
print("That's cool, have a good one!")
else:
show_score(best_attempts)
break
else:
if user_guess > target_number:
print("It's lower!")
else:
print("It's higher!")How it works
- The game welcomes the player and asks if they want to play.
- It picks a random target number in a specified range.
- Each round continues until the user guesses the correct number or quits.
- The program compares the user's guess to the target number and provides higher or lower hints.
- Invalid inputs are handled with
try-exceptso the script does not crash. - The best score is updated only when the user beats it. If there is none yet, the first win becomes the best score.
Step 6: Running the Game
Now, let’s call the start_game() function to start everything when the script runs:
if __name__ == "__main__":
start_game()Why is this needed?
This line ensures that our game runs only when the python script is executed directly, not when it is imported as a module.
- If
__name__ == "__main__"is true, the script was run directly, so it starts the game. - If it was imported elsewhere, the
start_game()function does not execute automatically.
This is a best practice for writing Python programs, making your code modular and reusable.
Example Run (What You’ll See)
Hello, traveler! Welcome to the game of guesses!
What is your name? Sam
Hi, Sam, would you like to play the guessing game? (Enter Yes/No): yes
There is currently no best score, it's yours for the taking!
Pick a number between 1 and 10 (or type q to quit): 6
It's lower!
Pick a number between 1 and 10 (or type q to quit): 3
It's higher!
Pick a number between 1 and 10 (or type q to quit): 4
Nice! You got it!
It took you 3 attempts.
New best score saved.
Would you like to play again? (Enter Yes/No): no
That's cool, have a good one!Final Code: Number Guessing Game
Here’s the complete code for your Number Guessing Game:
import random
import json
import os
import sys
SCORE_FILE = "best_score.json"
MIN_NUM = 1
MAX_NUM = 10
def load_best_score():
if not os.path.exists(SCORE_FILE):
return None
try:
with open(SCORE_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
best = data.get("best_attempts")
if isinstance(best, int) and best > 0:
return best
return None
except (OSError, json.JSONDecodeError):
return None
def save_best_score(best_attempts):
try:
with open(SCORE_FILE, "w", encoding="utf-8") as f:
json.dump({"best_attempts": best_attempts}, f)
except OSError:
pass
def show_score(best_attempts):
if best_attempts is None:
print("There is currently no best score, it's yours for the taking!")
else:
print(f"The current best score is {best_attempts} attempts.")
def start_game():
best_attempts = load_best_score()
print("Hello, traveler! Welcome to the game of guesses!")
player_name = input("What is your name? ").strip() or "Player"
wanna_play = input(f"Hi, {player_name}, would you like to play the guessing game? (Enter Yes/No): ").strip().lower()
if wanna_play != "yes":
print("That's cool, thanks!")
sys.exit(0)
show_score(best_attempts)
while wanna_play == "yes":
attempts = 0
target_number = random.randint(MIN_NUM, MAX_NUM)
while True:
raw = input(f"Pick a number between {MIN_NUM} and {MAX_NUM} (or type q to quit): ").strip().lower()
if raw in ("q", "quit"):
print("That's cool, have a good one!")
sys.exit(0)
try:
user_guess = int(raw)
if user_guess < MIN_NUM or user_guess > MAX_NUM:
raise ValueError(f"Please guess a number within the specified range ({MIN_NUM} to {MAX_NUM}).")
except ValueError as err:
print("Oh no! That is not a valid number. Try again...")
print(err)
continue
attempts += 1
if user_guess == target_number:
print("Nice! You got it!")
print(f"It took you {attempts} attempts.")
if best_attempts is None or attempts < best_attempts:
best_attempts = attempts
save_best_score(best_attempts)
print("New best score saved.")
wanna_play = input("Would you like to play again? (Enter Yes/No): ").strip().lower()
if wanna_play != "yes":
print("That's cool, have a good one!")
else:
show_score(best_attempts)
break
else:
if user_guess > target_number:
print("It's lower!")
else:
print("It's higher!")
if __name__ == "__main__":
start_game()Wrapping Up
Congratulations, you’ve built a complete Number Guessing Game using Python.
This project helped you learn:
- Random number generation with
random.randint - Loops, using a
whileloop to keep the game running - Error handling, using
try-exceptto handle invalid input - User input validation, ensuring guesses stay in the specified range
- Conditional statements, using
if-elif-elseto give hints - File-based persistence, saving a best score for future runs
- Modular programming, using
if __name__ == "__main__"
Next Steps: Enhance the Game
- Expand the specified range, for example, 1 to 100
- Give the player a limited number of guesses
- Add difficulty levels, Easy, Medium, Hard
The best way to learn Python is by building fun projects, so keep experimenting.