Are you new to programming and looking for your first project to help solidify your understanding? A great way to start is by building a simple game. In this blog, we’ll walk you through creating a "Guess the Number" game using Python. This project will help you get hands-on experience with basic Python concepts like loops, conditionals, and input handling.
The "Guess the Number" game is a simple yet effective way to practice your coding skills. The game randomly selects a number between 1 and 100, and your goal is to guess the number. The game gives feedback after each guess—whether it’s too high, too low, or correct.
To help you better understand the process, we’ve created a step-by-step video that demonstrates how to build this game. You’ll find text instructions alongside Python code execution, making it easy to follow along.
Below is the Python code for creating the "Guess the Number" game. You can follow along with the video to build this project.
import random
# Welcome message
print("Welcome to the Guess the Number Game!")
print("I have picked a number between 1 and 100. Can you guess it?")
# Generate a random number
number_to_guess = random.randint(1, 100)
attempts = 0 # Counter for guesses
# Loop for user to keep guessing
while True:
try:
user_guess = int(input("Enter your guess: "))
attempts += 1 # Increase attempts
if user_guess < number_to_guess:
print("Too low! Try again.")
elif user_guess > number_to_guess:
print("Too high! Try again.")
else:
print(f"Congratulations! You guessed the number in {attempts} attempts.")
break # Exit loop if guess is correct
except ValueError:
print("Invalid input! Please enter a number.")
To run this code on your own machine:
Once you’ve built the basic game, you can enhance it by:
This simple project is a great starting point for anyone new to Python programming. It introduces foundational concepts like loops, conditionals, input handling, and randomness. By following this guide and the accompanying video, you’ll be able to build your own "Guess the Number" game confidently.