Making A Number Guessing Game In Python

number guessing game in python
Table of Contents

Today we will make a number guessing game in python. A number guessing game is a user enters a number and it should match the number which the program has created.

Our number guessing game will little different the user will enter a number than if the number is wrong than the user will get a hint like your number is greater or lesser than the answer and the user will have 3 chances to answer the right answer.

It is a simple and easy project you should try to create this project on your own if you can’t than you can follow this article I will give the source code for this project.

If you are intrested in more projects like this than you can check out my projects that I have created at coderzway projects.

Let’s make a number guessing game in python

I hope you have knowledge of python basics and you have python installed on your machine if you don’t have python installed you can go to the python website and download – download python and remember to check add python to path while installing.

1. Create python file

Now first you need to create python file with .py extension you can go to any folder and create a python file and you need to open this file in a text editor I use vs code you can use any editor you can download vs code from here – download vs code.

2. Copy and paste

Now you have created the file now you can paste this code for number guessing game

				
					# Number guessing game by coderzway

import random

chances = 3
num = random.randint(1,15)
is_matched = False

while is_matched == False:
    if chances < 0:
        print(f'Try next time your chances are finished the number was {num}')
        break
    gNum = int(input('Guess a number between 1 to 15 : '))
    if gNum == num:
        print(f'Congrats you have guessed the number {num}')
        is_matched = True
    elif gNum != num:
        chances = chances - 1
        if gNum > num:
            print('Your number is greater than the answer try with a small number')    
        elif gNum < num:
            print('Your number is lesser than the answer try with a big number')    
				
			

How does this works

So I have used the random python module and it generates a random number between 1 to 15 you can increase this than asks for your input than it goes into the while loop checking if the answer is right or you have finished your chances if not it continues to ask the answer.

After this you can run this file by opening command prompt or terminal at the file location and type this in the command prompt python filename.py

This will run the file and this is the output

Output

number guessing game in python

How to use

1 – Enter a number and it will check if the number is correct
2 – If it is wrong you will get a hint and you have 3 chances

So this was number guessing game in python I know it was simple and easy but you can expand this project. If you want more projects do subscribe to coderzway newsletter and thank you for reading see you next time.

Share This Post
Share on facebook
Share on twitter
Share on email
Share on whatsapp

This Post Has 6 Comments

  1. M106.COM

    I was very pleased to find this site. This is an intelligent and well written article, you must have put a fair amount of research into writing this. Thank you

  2. Webmaster INFO

    Thank you for the post on your blog. Do you provide an RSS feed?

  3. We Love Google P:)

    I beloved as much as you’ll obtain carried out right here. The sketch is tasteful, your authored subject matter stylish. nonetheless, you command get bought an nervousness over that you would like be delivering the following. sick unquestionably come further before again as exactly the same just about a lot ceaselessly within case you defend this increase.

Leave a Reply

Subscribe To Our Newsletter

Get updates and learn from the best

Latest Guides & Articles