Home > Article > Backend Development > Simple Number Guessing Game in Python
This Python program is a fun and engaging Number Guessing Game where the user has five chances to guess a randomly generated number between 1 and 100. Here's a breakdown of how the game works and how the code operates:
Welcome Message
When the game starts, the user is greeted with a playful message:
"Hello! This is Isaeus Guiang, and this is a number guessing game. You have 5 chances to guess the number, or... you will die :)."
Random Number Generation
The program generates a random integer between 1 and 100 using random.randint(1, 100). This number will be the target the user must guess.
User Input & Validation
Feedback on Guesses
After each guess, the program provides feedback:
Game Over
Random Number Generation
The game uses random.randint(1, 100) to create an unpredictable number, ensuring a fair and exciting challenge for every playthrough.
Input Validation
The code prevents invalid inputs (like letters or symbols) from counting as attempts, using a try-except block to handle errors gracefully.
Attempts Tracking
The guess_counter variable keeps track of how many guesses the user has made, ensuring the user gets exactly five chances.
Dynamic Feedback
The game provides helpful hints after each incorrect guess, guiding the player toward the correct number.
Humorous Twist
The playful messaging adds a fun, lighthearted tone to the game, making it more entertaining.
Hello! This is Isaeus Guiang, and this is a number guessing game. You have 5 chances to guess the number, or... you will die :).
Attempt 1/5 - Your Guess :) -> 50 Too high! Try again. Attempt 2/5 - Your Guess :) -> 25 Too low! Try again. Attempt 3/5 - Your Guess :) -> 37 Too low! Try again. Attempt 4/5 - Your Guess :) -> 43 Too high! Try again. Attempt 5/5 - Your Guess :) -> 40 Oops, sorry, the number was 42. You are going to die :).
Attempt 3/5 - Your Guess :) -> 42 The number is 42, and you guessed it right in 3 attempt(s)!
This program is a great way to practice basic Python concepts, including loops, conditionals, user input, and error handling, while also enjoying a fun guessing game!
Feel free to use my code on my GitHub: https://github.com/asiguiang/Simple-python-game-Number-Guessing?tab=readme-ov-file#number-guessing-game-overview
About the Author:
Isaeus "Asi" Guiang
Regional Captain of AWS Cloud Clubs Philippines and a dedicated student at the Polytechnic University of the Philippines. With a strong passion for cloud computing and cybersecurity, Asi has played pivotal roles as the Former Captain, of AWS Cloud Club - PUP Manila, driving collaboration and innovation in the tech community.Currently pursuing a Bachelor of Science in Computer Science, Asi has a wealth of experience, including being an AWS re/Start Course Coordinator Intern and a Soft Skills Review Instructor. He teaches practical AWS workshops and is now a Security Risk Assessment and Compliance Intern at Globe, enhancing his expertise in the field.
Currently studying and focusing on Cloud Financial Operations and Security Compliance.
The above is the detailed content of Simple Number Guessing Game in Python. For more information, please follow other related articles on the PHP Chinese website!