6 Iteration: while-loops

Our initial programs had all values fixed in the code. For example, to change the number of people, we had to change the code and run it again. With the input function we can write programs that obtain the data from the user, and so the code doesn’t have to be changed every time the data changes.

In the previous activity you used the input function to obtain the number of people. Let’s also use it to ask the user for the prices of the items ordered. Since we don’t know in advance how many items were ordered and since we are not iterating over a given list anymore, we need a new form of iteration: an infinite loop that keeps asking for a price until the user types some special text, for example ‘stop’. We also need to convert the string returned by the input function into a number we can add to the expenses. The algorithm is:

  1. let the expenses be zero
  2. forever repeat the following:
    1. let answer be the reply to the question “Price of ordered item?”
    2. if the answer is equal to “stop”:
      1. exit the loop
    3. otherwise:
      1. let the price be the answer converted to a decimal number
      2. add the price to the expenses

Here is the corresponding Python code. I’ll explain it after the activity.

Activity 6.1

Test the code: provide any values you wish (e.g. 4.5 and 2), followed by stop. Don’t forget to press RETURN or ENTER after each number and after stop. The total displayed should be the sum of your values.

In Python, the while-loop

while condition:
    block

keeps executing the block while the condition is true. In our example the condition is always true (note the initial uppercase in line 2), which leads to an infinite loop. To end the program, at some point we have to break free from the loop with the appropriately named break instruction. At that point the computer starts executing whatever code follows the loop.

Food and drink prices may be in pence or cents, so I need to convert the user’s input string to a decimal number (called a floating point number in Python), using in line 7 function float instead of function int.

In line 4, to check if two values or variables are equal, we write two equal signs, because a single equal sign is the assignment instruction.

Activity 6.2

Write a complete program that:

The program that asks for and computes the expenses is repeated next. To reduce the amount of code to type, go back to Section 5, select with the mouse the code you need and press Ctrl-c on Windows or Cmd-c on macOS to copy the code. Then come back here, click on where you want to paste it and press Ctrl-v or Cmd-v. Finally, you need to add one line of code to calculate the VAT and change the line that calculates the total bill.

A simple test to check the VAT is correctly calculated is to have a single expense of 100 and only 6 people (to not add any tip). The total bill should be 120.

You can compare your solution to mine.

6.1 The oldest algorithm

The while loop condition doesn’t have to be always true, of course. As an example, here is one of the oldest algorithms known, Euclid’s algorithm. It finds the greatest common divisor of two integers greater than zero: that’s the greatest number that divides both without any remainder. For example, the greatest common divisor of 3 and 7 is 1, and of 21 and 49 is 7. This is needed to simplify fractions, for example. Explaining the maths that make the algorithm work is beyond the scope of this tutorial.

Activity 6.3

Translate the algorithm below to Python. One instruction was already translated for you. Don’t forget to test your code with inputs 3 and 7 (the result should be 1), and inputs 21 and 49 (the result should be 7).

You can compare your solution to mine.

For a while-loop to end without using the break instruction, the condition must become false. Here, the while-loop ends when variables n and m have the same value. Therefore it doesn’t matter which one is printed.

Functions Start Summary