do while Python loop is used to repeat a block of code while a boolean condition remains true. The Python syntax for while loops is
Most programming languages include a useful feature to help you automate repetitive tasks. This feature is referred to as
loops. For example, if you want to write a program that prints out individually the names of every student in a list, you may want to use a loop. Or if you want to run multiple calculations on values in a list, you may also want to use a loop.
In this tutorial, we are going to break down the
do while loop (which is officially called a
while loop) in Python. A while loop can be used to repeat a certain block of code based on the result of a boolean condition. The code in the while block will be run as long as the statement in the while loop is True.
We’ll also run through a couple of examples of how to use a
do while loop in Python.
For Loop Refresher
Loops are useful in a vast number of different situations when you’re programming. As a result, Python has two built-in functions that allow you to create loops: for and while.
In Python, for loops are used to repeat the execution of code based on a loop counter. So, if you want to iterate through a list, or run a block of code a certain number of times, you may want to use a for loop.
Here’s an example of a for loop in action that iterates through a range of values:
for i in range(0, 3): print(i)
The output from our code is as follows:
0 1 2
The for loop sets
i as the iterator, which keeps track of how many times the loop has been executed. The loop runs three times, or once for each item in the range of
This is an example of a basic loop that runs a certain number of times. But what if we want our loop to run when a condition evaluates to True? That’s where the do while loop comes in.
Do While Python
Python while loops allow you to run a certain block of code when a statement evaluates to true. Here’s the syntax for creating a while loop in Python:
while [our condition is True]: [run code]
Our loop will continue to run until the condition being evaluated is equal to false. In many programming languages, this is called a
do while loop, but in Python we simply refer to it as a
Let’s use an example to illustrate how a while loop works in Python. We are going to create a program that asks a user to guess the magic number, and continues to run until the user guesses correctly.
If the user guesses the number incorrectly, the loop will keep going, and if the user guesses the correct number, the loop will stop.
Here’s the code for our example
while loop program:
user_guess = 0 magic_number = 5 while user_guess != magic_number: print('What is the magic number?') user_guess = int(input()) print('You have correctly guessed the magic number!')
There is a lot going on in this code, so let’s break it down. In the first two lines, we declare two variables. The
user_guess variable will be used to store the number our user inputs into the program. The
magic_number variable stores the number the user is attempting to guess.
On the next line, we declare our
while loop. This loop checks if the variable
user_guess is not equal to
magic_number, and if these values are not the same, the loop will run. So, in other words, if our user has not guessed the correct magic number, the while loop will execute.
In our while loop, we print the statement
What is the magic number? and then we use the
input() function to request a guess from the user.
As soon as the user types in a number and presses
enter, our program will check to see if the
while condition is still true. Here’s what happens if we guess the wrong number:
What is the magic number? 1 What is the magic number? 2 What is the magic number? 3 What is the magic number?
As you can see, if we guess the wrong number, the program executes the
while loop again. So, our loop will run like an infinite loop—one with no end—until we enter the right number after which point our loop body will stop running and our program will move on.
Here’s what happens if we guess the correct number:
What is the magic number? 5 You have correctly guessed the magic number!
After we guessed the correct number,
user_guess was equal to
magic_number and so our while loop stopped running. Then, our program printed out the message stating that we had correctly guessed the magic number.
Remember that when you’re working with
input(), you may need to convert the values that you are receiving from a user. In our case, we had to use
int(input()) because we were gathering numbers from a user. If we wanted our values to be strings, though, we would not have to convert our values.
Do While Loop Example
Now that we know the basics of while loops in Python, we can start to explore more advanced loops. In this example, we are going to create another guessing game, but this time we are going to include a few additional features to make it more functional for users.
The specifications for our program are as follows:
- The magic number must be automatically generated
- The user should only get three attempts to guess the magic number
- If the user guesses the correct number, they should receive a message
Let’s build our program! Firstly, we are going to import the
random module using
import, which allows us to generate random numbers. Then, we are going to create a variable that stores a randomly-generated number. We can do so using this code:
import random magic_number = random.randint(1,20)
In our code below, we are going to define a while loop, like we did above, which receives our user’s guess. But in this example, we are going to use
while to check how many times a user has guessed the number. If that number is more than 4, the loop will not run. Here’s our code:
import random magic_number = random.randint(1,20) attempts = 0 while attempts < 5: print("Guess a number between 1 and 20:") guess = int(input()) attempts = attempts + 1 if guess == magic_number: break print("You have guessed the magic number!")
There’s a lot going on in this code, so let’s break it down. Our
while loop checks if a user has attempted to guess the loop fewer than four times, and if they have, the code within our loop will run. Then, the message
Guess a number between 1 and 20: will be printed to the console, and the user will be prompted to guess a number.
On the next line, we increase the number of attempts a user has had by 1. This allows us to keep track of how many guesses a user has had.
We then check to see if the user’s guess is equal to the
magic_number that our program generated earlier. If
guess is equal to
magic_number, our while loop will stop because we have used a
break statement. You can learn more about the Python break statement in our guide here.
Finally, once our
break statement is executed, our loop will stop and the statement
You have guessed the magic number! will be printed to the console.
Let’s test our code to see if it works. When we guess a number incorrectly, our loop runs again like this:
Guess a number between 1 and 20: 1 Guess a number between 1 and 20: 2 Guess a number between 1 and 20: 3
But when we guess the number correctly, our program returns the following:
Guess a number between 1 and 20: 5 You have guessed the magic number!
There’s a lot more we could do with this code, but the above example illustrates another situation where a do while loop would be useful in Python.
While loops in Python (which are often called
do while loops in other languages) are used to execute a certain block of code while a statement evaluates to true. For example, you may want to use a while loop to check if a user’s password is correct on a login form, or if they have guessed the right number in a guessing game.
Now you’re ready to start writing while loops like a pro in Python!