Table of Contents[Hide][Show]
We will start by understanding while loops and advance through building an interesting guessing game in Python.
All previous concepts will come into play so I will recommend you to revise previous lectures before moving further.
Let’s dive into it.
While Loops
We are going to learn how to use while loops in python. We use while loops to execute a block of code multiple times and they are often useful in building interactive programs and games.
So, let’s get started with the basics. To write a while loop, we write a condition after the “while” function.
Let’s introduce a variable “i” and set it to 1
i = 1
while i <= 5:
print(i)
Now, “i” will be incremented by 1 as the above loop will be an infinite loop if the value of “i” is not changed.
i = i + 1
This program will print the value of “i” until the above condition is true. As soon as the value of “i” becomes greater than 5, the loop will break.
To understand, how this works let me add another statement with no indent.
print("done")
As it is not indented, it will not be executed with the while loop.
Here is how it will work:
Let’s play with the while loop a little.
I am adding another string in the while loop with an “asterisk” multiplying with “i”. Let’s see how it will impact our program.
i = 1
while i <= 5:
print('*' * i)
Now, it will print asterisks until the number of asterisks exceeds 5.
Now, we are putting the while loop to use. Let’s build a guessing game.
Guessing Game using While Loops
The concept is that the program saves a secret number in its memory and asks us to guess it. We have 3 attempts to guess that number. If you guess the number, you will be congratulated. Let’s put our heads together.
We are introducing a variable for our secret number.
secret_number = 6
Now, we have to write a while loop to repeatedly ask the user to guess the number. Before that, we need to introduce another variable to set the number of attempts allowed.
guess_count = 0
while guess_count < 3:
guess = int(input('guess: '))
guess_count +=1
if guess == secret_number
print("Congratulations! You won")
This program will take integer input from the user and save it in “guess”. It will then compare it with the secret_number using the comparison operator and if both are equal, it will return “Congratulations!
You won”. But this program has some flaws. If you guess the number on the first attempt, it will still ask you to guess another two times after congratulating you. This problem can be solved by using the “break” statement to break the loop.
Have a look!
Now, we are going to add an “else” statement which will execute if you are unable to guess the number in the three attempts.
Now, in this case, we will add an else statement out of the while loop which will be executed if the loop is not broken with if statement.
I hope this was a fun activity for you. You can indulge yourself in similar activities to get proficient in Python.
Wrap Up
This was the end of this lecture. I hope you enjoyed it so far. Meet you in the next one. Keep practicing until then.
Leave a Reply