Python Project for Beginners Guess the Number Game

• Post author:

In this project, we are creating a simple Guess the Number Game for beginners to practice and improve their coding skills. By completing this project you will be able to learn

• How to Generate random numbers using the `random` library
• Asking for user input using the `input()` function.
• Converting user input to integers using the `int()` function
• Using conditional statements to compare the user’s guess with the randomly generated number
• Using a `while` loop to repeat the guessing process until the user correctly guesses the number.
• Printing output messages to the console using the `print()` function.
• Creating a simple user interface that prompts the user for input and provides feedback on their guesses.
• Basic debugging techniques and how to handle errors that might occur in the program.

What this Game is about?

This is a game where the computer generates a random number between 1 and 100, and the player tries to guess the number. The computer provides feedback on whether the player’s guess is too high or too low until the player correctly guesses the number.

So let’s begin the project.

Importing modules

The first step in this project is to import the necessary library. We are using a random module to do this project. We can import it by using the below command

``````
import random
``````

This will import the random library to our project and the next step is to write a code for the project. You can find the source code below and after that, we will explain the project

``````
import random

print("Welcome to the Guess the Number game!")

print("I'm thinking of a number between 1 and 100. Can you guess it?")

# generate a random number between 1 and 100
number = random.randint(1, 100)

# keep track of the number of guesses
num_guesses = 0

while True:

guess = input("Enter your guess: ")

# check if the guess is a valid number
try:
guess = int(guess)
except ValueError:
print("Invalid input. Please enter a number.")
continue

# increment the number of guesses
num_guesses += 1

# check if the guess is correct
if guess == number:
print(f"Congratulations, you guessed the number in {num_guesses} tries!")
break
elif guess < number:
print("Too low! Guess again.")
else:
print("Too high! Guess again.")
``````

Explanation of this program

The project is written in Python and starts by importing the `random` module, which is used to generate a random number that the player will try to guess. The program then prints a welcome message and tells the player that it is thinking of a number between 1 and 100

And after that, we are using a `while` loop here that will continue running until the player guesses the correct number. Within the loop, the player is prompted to enter a guess, and the program checks to make sure that the input is a valid integer. If the input is not a valid integer, then the program will print an error and ask again to enter an integer

After the player enters a valid integer, the program compares the player’s guess to the random number that it generated at the start of the game. If the player’s guess is correct, the program prints a congratulatory message, which tells the player how many guesses they made. The `while` loop is then exited, and the program ends.

If the player’s guess is incorrect, the program prints a message that tells the player whether their guess was too high or too low. The loop continues, and the player is prompted to enter another guess.

You can download the source code from here

Latest posts by Publisher (see all)

Publisher

Publisher @ideasorblogs