Python Number Guessing
Number Guessing Game

Python Number Guessing

In this article, we are going to make another basic python project on "Number guessing"

Our assumptions:

  1. A total of 3 chances will be given to the user
  2. For each input, players will get acknowledgment whether their predicted number is greater or bigger than the actual number
  3. After all, chances are gone the program will terminate with the actual number

Now let's begin with the coding part:

1stly we need python's "random" library to choose a number randomly from the given range

import random        

Now we will print the total number of changes we want to give the user

print("Total Chances 3")        

Now we will generate a random number. For this inbuilt function "randint" is used from a random library. Here rand = random & int = integer number. So, the output will be a random integer number under a given range.

num = random.randint(0,100)        

Here the range (0,100) will include the values from 0 to 100 including both 0 & 100.

Let's print the random number for our convenience:

print(num)        

Now we will go in the loop for the range of total chances we want to give

for i in range (2,-1,-1):        

Here, we have specified 2 because "i" will iterate from 2 to 0 i.e., a total of 3 times and after 1st input is the wrong user will have remaining chances of 2. this can be seen easily in the output

Inside The Loop:

  • Taking input from user:

x = int(input("\nGuess the number between 0 to 100: "))        

  • Checking for if the number is Greater than the actual number, printing chances left i.e., "i" and acknowledging

if x > num:
    print("Fortunately, you guessed it bigger")
    print("Number of chances left: ",i)        

  • Rechecking for if it is equal to the number and acknowledging. Here also we will print chances left i.e., "i". Also, a break statement will be used here for if the condition is true then the system will exit from the loop.

elif x == num:
    print("Yep, you guess it correct. Congo!!")
        

  • Finally is both conditions are false then it means that guessed number is smaller than the actual number. Therefore, acknowledging user and printing chances left i.e., "i".

else:
    print("Nope, you guessed it smaller")
    print("Number of chances left: ",i)        

End of Loop

Furthermore, if the user fails to guess the number, the system will give output:

if x!=num:
    print("The Secret Number is: ",num)
    print("Number of chances left: ",i)
    break        

(OPTIONAL) Some Designing in the beginning:

print("*********************************")
print("             WELCOME")
print("    The Number Guessing Game")
print("*********************************")        

Our complete code looks like this:

print("*********************************")
print("             WELCOME")
print("    The Number Guessing Game")
print("*********************************")
import random
print("Total chances 3")
num = random.randint(0,100)
print(num)
for i in range (2,-1,-1):
    x = int(input("\nGuess the number between 0 to 100: "))
	if x > num:
	    print("Fortunately, you guessed it bigger")
	    print("Number of chances left: ",i)
	elif x == num:
	    print("Yep, you guess it correct. Congo!!")
	    print("Number of chances left: ",i)
	    break
	else:
        print("Nope, you guessed it smaller")
        print("Number of chances left: ",i)
if x!=num:
    print("The Secret Number is: ",num)        

Output:

No alt text provided for this image

Here is the link to my Github Profile

Hope you all find the content useful. Have a good time

Siddhant Garg

Quality Engineer @IBM | AWS Certified | Azure Certified | SAFe 6 Certified | ISTQB Agile Tester Certified | Salesforce AI Certified | Salesforce Associate Certified | Lean Six Sigma & RPA Dynamics Certified

3 年

Great work buddy??

要查看或添加评论,请登录

Kartikey Garg的更多文章

  • Crypto Price Prediction Report

    Crypto Price Prediction Report

    Disclaimer This project was developed with the assistance of an AI companion, Microsoft Copilot. The AI generated all…

  • The Growing Generative AI

    The Growing Generative AI

    Warm Greetings to all, let's proceed further without taking any further time in welcoming because as you read this…

  • Encourage employees for Wide Knowledge

    Encourage employees for Wide Knowledge

    Hello, working professionals, I hope you all are having a great day at your job. My name is Kartikey Garg, a fresher…

  • Inputting data in Google Search using Selenium

    Inputting data in Google Search using Selenium

    Hello all, in this article, we will focus on the use of Selenium in searching for any word. Let us begin: 1st of all we…

  • Web login using Selenium

    Web login using Selenium

    In this article, we will study the use of Selenium. We will practice login into Facebook.

  • Magic 8 Ball Game

    Magic 8 Ball Game

    Today we are code for the "Magic 8 ball" game in Python Our Assumptions: A list of answers representing 8 balls The…

  • The Dice Rolling Simulator

    The Dice Rolling Simulator

    In this article, we are going to form the small and simple project of "Rolling Dice in Python". I will be explaining…

  • Reversing Time: When Time goes Negative

    Reversing Time: When Time goes Negative

    Hello Everyone, hope all are doing well and great, I was eager to share my own thoughts regarding time travel. Today's…

社区洞察

其他会员也浏览了