While Loop in Python

In this tutorial, we’ll learn about loops in Python. We’ll see the “while-loop” and how to use it to solve our problem…

Sari Lakkis
4 min readMay 16, 2021
Photo by Joshua Sortino on Unsplash

For this tutorial, we have a simple problem to solve. We need to calculate the price of items purchased in a certain marketplace or store.

The user of our app keeps entering the price of items purchased and their quantity and when he is done we display the total price of all items.

The input stops when the user enters any negative number for the price.

The formula to calculate the total price is:

Total Price = sum of (item prices * item number)

The Source code of this article can be downloaded from my GitHub Python repository.

Let’s start!

How to solve such a problem?

In this kind of problem, we have repetition. We are repeating the same process once after once.

Our small process is:

1- Read the item price from the user

2- Read the item’s number from the user

3- Multiply the price by number and add it to the total.

We are repeating this process until the user enters a negative number for the item price. Finally, we display the total price we already calculated.

To do this repetition of a certain part of the code, we’ll use loops.

What is a loop?

Loop is a structure that repeats a number of instructions until a specific condition is met.

We’ll use the while loop. The while loop, as its name says, will repeat the sequence of instruction while a certain condition is true. We specify the condition we want according to our needs.

In Python, the while-loop condition is directly written after the while keyword like in the if-statement, example:

while i < 10:

Statements that should be repeated are written with an indentation under the while. Example:

i = 1
while i <= 5:
print(“I love Python”)
i = i + 1

In the above loop, we have a variable “i” with a value 1. In the while loop, we are printing the sentence “I love Python” and then incrementing “i” by 1.

The condition “i <= 5” means that we keep repeating the instructions in the while block until as long as “i” is less than or equal to 5.

This will print the sentence “I love Python” five times.

Each time we execute the statements in the while loop, we call it an iteration. So in our example, above we have five iterations since the loop will start when i is one and ends at i equals five.

After the loop ends, statements below it will continue to be executed normally.

Writing the Code

Let’s create a folder called “ForLoopInPython” and open it in VS Code. Then create a new file called “main.py” to hold our code.

We’ll first create a variable total with the value “0.0“ to hold the total price for all purchased items. Then we read the first price from the user before we enter the loop to have something to test against in the while loop condition.

total =0.0
price = float(input("Enter price (negative number to stop): "))

The loop will check if the value of the price is positive, if this is the case, we are good to go.

while price >= 0:

Inside the loop, in every iteration, we read the number of items and store it in a variable “number” and update the total value by adding to it the price * number value.

At the end of each iteration we should read the next value of the price then we return back to the start of the loop where we check again if the condition is still valid and repeat the execution of the instructions.

When the condition is no longer valid, we skip the loop block and jump to execute the statements after. That’s in our case when the user enters a negative number for the item price.

The full code is shown below:

total = 0.0
price = float(input("Enter price(negative value to stop): "))
while price >= 0:
number = int(input("Enter the number of items: "))
total += price*number
price = float(input("Enter the item price: "))
print("The total price is:", str(total))

Testing the Application

Run the code and enter the value for the price, if you entered a positive value then the program will ask you to enter the number of items which means we’ve entered the loop.

The program keeps asking for another item price until we enter a negative value and this is where the loop ends and the execution jumps to the statements after the loop.

Conclusion

In this tutorial we learned about loops and where to use them, we wrote a Python program to solve our problem that required a certain code repetition using the while loop.

There is another type of loop in Python: the for-loop. It is used in manipulating collections in Python like (lists, sets, and other types). This will be our next tutorial subject, see you there!

--

--

Sari Lakkis

Professor | Consultant | Researcher. I give advice on strategy, design, and implementation of software that solves business problems to companies and startups.