Number guessing game - python

hello guys in today's blog we're going to learn Python basics with this one project after this project you can understand Python basics very well. the foundation is very important to becoming a pro in anything not only coding. 

ok without wasting time now let's see the project

project outline:

this project is very simple we're going to tell our system to pick any number between 1 to 10 after that we want to find that number that's it. let's code to this project

requirement of this project:

1. Visual studio code editor or any other(eg: Pycharm  , IDLE)


ok now let's see the code explanation

first, you need to import a random library why do we use this one to generate random numbers 1 to 10

import random

once you import random now you want to use this to generate random numbers between 1 to 10

here is how you can do this :

1. first assign variable 

2. then you have to tell to system choose between 1 to 10 for this 

here is how you can do these two functions :

chosen_number = random.randint(1,10)


once you complete this you should have to assign a chance player has to find the number which system chose

chance = 5



#This line means "Keep doing the following until you run out of chances".

while chance > 0:

#This line asks you to type in a number

    player_input = int(input("enter the number ::"))

#This line checks
#if the number you typed is the same as the
#computer's chosen number.

    if(player_input == chosen_number):

#If you guessed right,
#it says "You won" and the game ends.

        print("you won")
        break

#If your guess is higher than the chosen number,
#it says your guess is "too high".
#and takes away one chance.

    elif(player_input > chosen_number):

        print("too high")
        chance -= 1


#If your guess is not too high and not right,
#it must be too low.

    else:

#It tells you your guess is "too low"
#and takes away one chance.

        print("too low")
        chance -= 1

#After you've used all your chances,
#it checks if you didn't guess the right number.


if(chance == 0):

#If you didn't guess the right number,
#it says "you loss".

    print("you loss")


Now we have completed this project code. here is the full code :

import random

chosen_number = random.randint(1,10)


chance = 5


while chance > 0:

    player_input = int(input("enter the number ::"))


    if(player_input == chosen_number):

        print("you won")
        break

    elif(player_input > chosen_number):

        print("too high")
        chance -= 1

    else:

        print("too low")
        chance -= 1


if(chance == 0):

    print("you loss")

once you complete this project, the output will look like this :





Conclusion: 

I hope you found this project easy to understand and fun to work on! If you have any questions or run into any problems, don't hesitate to comment.  Happy coding 😊 

Make sure to follow me on Instagram. I'll be sharing coding and DSA stuff every day. Join me for cool coding projects and learning. Let's connect and learn together! ✌





Comments

Popular Posts