Number Guessing Game using Python

The number guessing game is a popular game among programmers. In the number guessing game, the program selects a random number between two numbers, and the user guesses the correct number. If you want to learn how to create a guessing game using Python, this article is for you. In this article, I will take you through a tutorial on creating a number guessing game using the Python programming language.

Number Guessing Game using Python

To create a guessing game, we need to write a program to select a random number between 1 and 10. To give hints to the user, we can use conditional statements to tell the user if the guessed number is smaller, greater than or equal to the randomly selected number.

So below is how you can write a program to create a number guessing game using Python:

import random
n = random.randrange(1,10)
guess = int(input("Enter any number: "))
while n!= guess:
    if guess < n:
        print("Too low")
        guess = int(input("Enter number again: "))
    elif guess > n:
        print("Too high!")
        guess = int(input("Enter number again: "))
    else:
      break
print("you guessed it right!!")
Enter any number: 2
Too low
Enter number again: 5
Too low
Enter number again: 8
you guessed it right!!

If the guessed number is lower than the randomly selected number, the user will see “too low”. If the guessed number is higher than the randomly selected number, the user will see “too high”. When the user guesses the correct number, “you guessed it right!!” will be displayed in the output.

Summary

So this is how you can write a program to create a guessing game using Python. It is a popular game among programmers. In this game, the program selects a random number between two numbers, and the user guesses the correct number. I hope you liked this article on how to create a guessing game using Python. Feel free to ask valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data๐Ÿ“ˆ.

Articles: 1501

11 Comments

  1. hello mr aman kharwal : i just want to tell you in simlpe with more proud ……………thanks a lot

Leave a Reply