top of page

While Looped in the Number Guessing Game

Oct 28

3 min read

0

8

0

I’ve been refreshing my Python skills lately using Codedex and the best way for me to learn is by breaking down the code into simple, everyday language, or what I like to call techsplaining. So here's me techsplaining the while loop and conditions.



The Challenge


Number Guessing Game Instructions
Number Guessing Game Instructions

The goal was to create a program that lets the user guess a secret number (which is 6) but limits them to only three tries.



My Final Code


Number Guessing Game Code
Number Guessing Game Code


Code Techsplained


Now let's dive into the code and understand what's really happening under the hood.


guess = 0
tries = 0

To start off, I created two empty buckets (variables assigned with 0). These buckets are placeholders and there's nothing in it for now. The guess bucket will hold the number the user inputs and the tries bucket will count how many guesses the user has taken.


print("Let's play the Number Guessing Game!")
print()

These two lines aren't required for the program to run but they add a nice touch. The first line acts like a header which tells the user what they're about to do. The second line just prints a blank line for spacing.


while guess != 6 and tries < 3:
	guess = int(input("Guess the number: ")
	tries +=1

This is the while loop and for this one in particular, it runs only when two things are true: the guess is not 6 and the number of tries is less than 3. These are the criteria (or conditions) for the loop to keep going. What that means in tech terms is that the while loop will only run as long as the condition(s) is TRUE.


At the start, both the guess and tries buckets are 0 so it meets both conditions and allows the loop to begin. Once inside the while loop, the user is asked to enter an integer. After that, the tries bucket goes up by one. The next time the loop runs, the tries variable will start at 1 instead of 0.


If the criteria aren't met (condition is FALSE) then the program skips the while loop entirely and moves on to the next section of code. This happens when the user correctly guess 6 or the user has already made three guesses.


if guess == 6:
	print("You got it!")
else:
	print("Sorry, you only get three tries.")

If the number in the guess bucket is 6, the program will display (or print): You got it!


Otherwise, it displays: Sorry, you only get three tries.


You might be wondering what if the user guesses wrong before using all three tries?

Well, they wouldn’t reach this part of the code yet because they’d still be inside the while loop since the criteria is still true.



Testing My Code


After several rounds of debugging, I finally tested both scenarios to make sure everything worked. It was satisfying to see both outcomes working exactly as expected.


Code successfully prints after 3 wrong guesses
Code successfully prints after 3 wrong guesses
Code successfully prints after making the correct guess
Code successfully prints after making the correct guess


Final Thoughts


This exercise helped me understand how loops check their conditions before running and why it's common practice to start variables at 0.


This was also a good reminder that Python runs from top to bottom and only moves on when the loop’s conditions are no longer met.



Related Posts

Comments

Share Your ThoughtsBe the first to write a comment.
bottom of page