Lesson 17
Number Guessing Game
Build a complete guessing game
In this lesson, we will create an exciting number guessing game that challenges players to guess a number chosen by the computer. This project will help you use your coding skills in a fun way!
2Game Overview🎯
The player will try to guess a randomly chosen number within a specific range. The program will provide feedback on whether the guess is too high, too low, or correct. This will involve using loops, conditionals, and random numbers.
3Setting Up the Game💡
First, we need to import the random module to help us choose a random number. Then, we will set a range for the guesses.
import randomnumber_to_guess = random.randint(1, 100)This code selects a random number between 1 and 100.
4Getting User Input🚀
Next, we will prompt the player to input their guess. We'll use a loop to keep asking until they guess the correct number.
guess = int(input("Enter your guess (1-100): "))This line allows the user to enter their guess, which we convert to an integer.
5Providing Feedback🔍
Now, we will compare the guess to the number. Use if-else statements to give feedback on the guess.
if guess < number_to_guess:
print("Too low! Try again.")
elif guess > number_to_guess:
print("Too high! Try again.")
else:
print("Congratulations! You guessed it!")This code gives helpful hints to the player based on their input.
6Completing the Game Loop⭐
Finally, we will wrap everything in a loop until the player guesses correctly. This ensures the game continues until they win.
while guess != number_to_guess:
guess = int(input("Enter your guess (1-100): "))
# Feedback code here...This loop keeps the game running until the player gets it right!
7Final Touches🧩
Once the game is working, think of ways to improve it. You could add a scoring system, limit the number of guesses, or even allow the player to choose the range of numbers. Get creative!
Enjoy coding your number guessing game and remember, practice makes perfect!
🎯 Practice Challenges
Master Number Guessing Game by completing all 3 difficulty levels
Number Guessing Game - Easy Challenge
Start with the basics. Perfect for beginners!
Number Guessing Game - Medium Challenge
Get more challenging. Build your confidence!
Number Guessing Game - Hard Challenge
Master this concept. Prove your skills!
🏆 Study a lesson for 2+ minutes to earn +10 TalentJR Points a day. Badges, certificates & prizes →
Ready to Master Number Guessing Game? 🚀
Complete the challenges, take the quiz and keep your learning streak going!
Keep learning
- ← Previous lessonLesson 16: Introduction to FunctionsPackage logic into reusable blocks
- Next lesson →Lesson 18: Capstone: Rock Paper ScissorsCombine everything into a full game
- Grade 7 coursePython Functions & DataPython coding course for kids.
- Student ToolsFree calculators for studentsPercentage, fraction, ratio and marks calculators with steps.
- CBSEClass 6 CBSE & NCERT study materialMaths, Science and Computer chapters with solutions.
- Future SkillsExplore future skillsAI, robotics and new-age skills explained for students.