Number Guessing Game Project in Python

This is a simple game where you have to guess a computer-generated number from 1 to 10. However, you can’t guess it all. You have only 4 chances to guess the correct number.

Number Guessing Project Python

To Build a Simple Number Guessing You need to follow the below steps

Step 1. Download Python

You only need Python to run this script. You can visit here to download Python and then you need to install a package named random.

pip install random

Step 2. Write the following code

Write this code in a Python file or directly copy this code and save the file with the name and .py extension python number_guessing.py

import random
import time
print('Hello World! Welcome to the game')
time.sleep(2)
n = random.randint(1, 10)
print('Number has been generated!!\nYou have 4 chances to guess the number')

count = 4

while count!=0:
a = int(input('Guess the number: '))

if a == n:
print("Yay! That's right. You won!")
break
elif a > n:
print('The number is less than ', a)
else:
print('The number is greater than ', a)
count=-1

Step 3. Run the code

After saving the code double-click on the file to run the program or simply open Command Prompt and write the following command.

python number_guessing.py

Demo:

128493914 936c4140 35ff 4fc6 9b5b 3f5c3f4cdc2a

Leave a Comment