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.
Hello sir , I am a beginner in python and your website help me to build my confidence . I want to become a data scientist.
Thank you so much, keep visiting ๐๐บ
its going in infinite loop for few numbers
I will check and update the code!
How can i contact you via WhatsApp, my number is +277******* i promise not to share your contact with anyone, i would like to share some of my work with you, and when you get time u would go through it maybe. its a very impressive project to me
Hi, I am always available on Instagram and Linkedin, please feel free to reach me there๐ค๐
Sir I am pursuing btech in artificial intelligence and data science currently in 2md year . My college isn’t that good so please I request you to guide me and tell of road map to became expert in AI , DS. ML. and deep learning and please specify where should I start .
I know python and c++ , basic dsa
Machine Learning Roadmap: https://thecleverprogrammer.com/2022/09/08/roadmap-to-learn-machine-learning/
Resources to Learn Deep Learning: https://thecleverprogrammer.com/2021/12/30/best-resources-to-learn-deep-learning/
What if you just give a certain number of trials so it doesn’t run to an infinite loop?
hello mr aman kharwal : i just want to tell you in simlpe with more proud ……………thanks a lot
always welcome ๐