Python for Beginners 13: While

In this lesson, we are finally going to learn about the while loop. Why finally? Remember our example of customized pizza cheese from the last lesson?

So far, our program can only take one single order. What if we want to give our customer the possibility to order more than one pizza? For this, we would need to execute our program several times. To avoid this, we can just create a loop, i.e. we include a part in the code that repeats the ordering process.

There are two ways to do it:

a) We don’t know how many pizzas our customers want so we just ask again after each order.
b) We know how many pizzas our customers will order beforehand, for instance because we asked them at the beginning, or we determine an amount of pizzas that has to be ordered.

If we want to know the final price to pay at the end, we need to create a variable called for instance final_price. This variable is a so-called accumulator because in it, we are going to accumulate the cost of all the pizzas we order.

  1. We set the accumulator (here: final_price) to 0.
  2. Inside the while loop (we’ll get into detail later), you define the accumulator as itself increased by whatever variable you want to accumulate inside. In our case, this means final_price = final_price + price. This means that every time we order another pizza, the price of the new pizza is added to the prices of our previous orders.

This aspect is the same in both ways of implementing the while.

Let’s have a look at the actual implementation. For both scenarios, I put an explanatory comment after everything in the code that is different from lesson 12.

a) We don’t know how many pizzas our customers want so we just ask again after each order.

Besides the accumulator that we have already discussed, there are only two more new aspects:

  • the while loop: while followed by a condition and a colon (just like an if in conditionals)
  • a variable that we use to create the condition: I called this variable resp, short for response, because in it we store the response of our customers – Do they want to make another order or not?
  1. At the beginning of the program, where we also initialized the accumulator, we set the variable resp to our desired initial value so that the condition of while is true.
    resp = 1
  2. Next, we create the while loop by writing while followed by the condition (variable == initial value) and the colon.
    while resp == 1:
    This assures that as long as long as the response to “another pizza” is yes (1), we repeat the ordering process.
  3. We copy and paste our code from last lesson (or whatever else we want our program to do as long as the while condition is true). Just make sure that the indention is correct. This works just like the conditionals with if and else, too.
  4. In the last line of the loop, i.e. before printing the final price, we ask the user for input on our resp variable.
    resp = int(input(“Do you want to order another pizza – yes (1) or no (2)? “)
    As long as the user responds 1 for “yes”, the condition defined in 2. is true, we stay in the loop and repeat the code from 3.


b) We know how many pizzas our customers will order.

For the second way of implementing a while, we need to consider three more aspects in addition to the accumulator.

  • the while loop: while followed by a condition and a colon (just like an if in conditionals)
  • How many times do you want to repeat the loop, i.e. how many pizzas will be ordered in total? Here, I named this variable order_pizzas and I ask the user for input.
  • How many times have you repeated the loop already, i.e. how many pizzas have already been order? As this variable does the counting, it is called a counter. In our example, it is called num_pizzas
  1. In the beginning of the program, where we also initialized the accumulator, we set the counter to 0.
    num_pizzas = 0
    Furthermore, if we want the user to determine the amount of repetitions, we have to ask for input here.
    order_pizzas = int(input(“How many pizzas do you want to order? “))
  2. Next, we create the while loop by writing while followed by the condition (counter < number of repetitions) and the colon
    while num_pizzas < order_pizzas:
    This assures that as long as long as we have ordered fewer pizzas than indicated in the beginning, we repeat the ordering process.
  3. We copy and paste our code from last lesson (or whatever else we want our program to do as long as the while condition is true). Just make sure that the indention is correct. This works just like the conditionals with if and else, too.
  4. In the last line of the loop, i.e. before printing the final price, we add 1 to the counter.
    num_pizzas = num_pizzas + 1
    This way, the counter always knows how often we have already repeated the process.

This has been a really long lesson, but I tried to break it down to make it easy to understand. If you have questions, just ask in the comment section below. Be proud, you have learned a fundamental aspect of programming! #mastery13

Leave a comment