Introduction
Making a project while learning a programming language is crucial to building the base on programming knowledge. Since we are learning python programming here, we'll create a fascinating number guessing game in python. This small project will help you a lot to clear some basic conditional logic related to the programming knowledge.
For your convenience, I've made two python programs for this project. The overall logic is the same for both. Only the difference is, One program is GUI based, the output will present graphically; another program is simple command-line based; users need to give input in the command-line.
Let's understand the logic of this Number Guessing Game.
👉Visit Also: Apple Catcher Game in Python - PyGame Project
How does the Game works?
The program generates a random number between 1 and 100, and store it in a variable named 'target'. You can't guess it previously. You'll get Ten chances to guess the number if it does not match with the 'target'.
Now a while loop will iterate Ten times for Ten chances; you need to guess the target number. If the guess number matches with the target, the program will print a Congratulation Message and the number of chances you took to get the actual result.
If your guessed number is less than the 'target' number then, the program will print "You've chosen less.". If the guess number is greater than the 'target', the program will print the opposite of the previous message.
When the loop will end and still you elusive to guess the target number, the program will give you a sad message and print the actual result along with this.
It's too simple, right? OK, let's move on to the next discussion.
Requirements and Installation
You need to install a simple python module called PyAutoGUI if you select the GUI-based number guessing game. Otherwise, it doesn't require any third-party module to be installed.
☛Install PyAutoGUI: pip install PyAutoGUI
GUI version of the Number Guessing Game in Python
Import Modules
Let's start writing your code by importing these two python modules.
import random
import pyautogui
The random module will generate a random number in a specific range. The PyAutoGUI will be used to show the output of this game in a simple graphical interface so that it looks more amusing.
Declare the variables
Now declare four variables that will be used in the upcoming code.
# Declaring some variables
lower = 1
upper = 100
attempt = 0
chances_left = 10
Generate a random number
The first line will generate a random number between 1 and 100.
target = random.randint(lower, upper)
# Show the first window
pyautogui.alert("Please Guess the number between \
1 and 100.\nYou've only 10 chances.")
The second line will show a window telling users to guess a number between 1 and 100.
The while loop: Iterates 10 times
The while loop will iterate until the number of attempts reaches 10. Each round 'attempt' variable will increase by one, and the 'chances_left' variable will decrease by one. The rest of the code inside the while loop will check if the guess number is equal to or less than or greater than the target number or not.
# The while loop will iterate until the user
# attempts 10 guesses
while attempt < 10:
attempt += 1
guess = pyautogui.prompt(f"Chances left: {chances_left}\nPlease\
guess the number.")
# Checks if the guess matches with the target number
# Convert the guess variable to an integer
if int(guess) == target:
pyautogui.alert(f"Congrats! You did it in {attempt} attempts.")
break
elif int(guess) < target:
pyautogui.alert(f"You've chosen less.")
elif int(guess) > target:
pyautogui.alert(f"You've chosen high.")
chances_left -= 1
Check the number of attempts
In the end, if the code found the number of attempts exceeds 10, it will show a message with the target number the user was trying to guess.
if attempt >= 10:
pyautogui.alert(f"Sorry! you lost.\nThe number is: {target}.")
A Simple version of the Number Guessing Game
In this part, the code is almost the same as the previous one; the only difference, this time, the output will print in the Command Prompt.
import random
# Declaring the variables
lower = 1
upper = 100
attempt = 0
chances_left = 10
target = random.randint(lower, upper)
print("Please Guess the number between \
1 and 100.\nYou've only 10 chances.")
# The while loop will iterate until the user
# attempts 10 times
while attempt < 10:
attempt += 1
guess = int(input(f"\nChances left: {chances_left}\nPlease \
guess the number: "))
if int(guess) == target:
print(f"\nCongrats! You did it in {attempt} attempts.")
break
elif int(guess) < target:
print(f"You've chosen less.")
elif int(guess) > target:
print(f"You've chosen high.")
chances_left -= 1
if attempt >= 10:
print(f"\nSorry! you lost.\nThe number is: {target}.")
Output
Please Guess the number between 1 and 100.
You've only 10 chances.
Chances left: 10
Please guess the number: 10
You've chosen less.
Chances left: 9
Please guess the number: 55
You've chosen less.
Chances left: 8
Please guess the number: 85
You've chosen high.
Chances left: 7
Please guess the number: 75
You've chosen high.
Chances left: 6
Please guess the number: 65
You've chosen less.
Chances left: 5
Please guess the number: 70
You've chosen less.
Chances left: 4
Please guess the number: 73
Congrats! You did it in 7 attempts.
👉Visit Also: Make Your own Quiz Game in Python - with CSV file
Summary
In this tutorial, you've learned to create a small logic-based project called the Number Guessing Game in Python. We tried two examples of this project, the GUI and the non-GUI version. Everybody can play it. Guessing numbers in a specific range in this game may help to refresh your mind.
So, start playing, and tell me in the comment section how many attempts you've taken to reach the target number. Do share the code with your friends and ask the same question to him/her. I ensure you'll enjoy it.
That's all for today, see you soon on the next topic.
Thanks for reading!💙
PySeek