3 Selection

Let’s imagine we are developing software for a restaurant, where a tip of 10% is added to the bill. Here’s the code, a simple sequence of assignments and output. For example, the assignment on line 3 states ‘let the tip be the expenses multiplied by the percentage’. In Python, the multiplication operator is the asterisk (*).

Let’s further assume the tip is to be added only for groups above 6 people. We need one more variable, to store the number of people in the group, and one new instruction to handle both cases: if the number of people is more than 6, then the percentage is 10%, otherwise it’s 0%.

Before I explain the code, let’s test it, to make sure it works as intended. Large software companies employ many testers to check their code. Good testing includes choosing enough inputs (preferably borderline cases) to exercise all possible conditions. In this case, we should at least test for 6 and 7 people, the borderlines where the tip percentage changes.

Activity 3.1

In line 2, change the number of people to 6 and run the code.

The bill should change from 59.4 to 54, as no tip is added for six people.

Let’s look more closely at the code. The selection instruction

if condition:
    block
else:
    block

chooses which block of code (one or more indented instructions) to execute. The computer first checks the condition after the if. If the condition is true, the computer then executes the first block, belonging to the if part. If the condition is false, the computer executes instead the second block. The indentation is needed to know which instructions belong to which part. Afterwards the computer executes any further non-indented instructions after the selection.

Notice there is a colon (:) at the end of the if and else lines. Forgetting the colons and forgetting to indent the blocks will lead to error messages.

3.1 And now for something not completely different

Usually there are many ways to solve the same problem. To add the tip only to groups over 6 people, we have selected whether to set the percentage to 0% or 10%, but we can instead change how the tip is calculated.

Activity 3.2

Change the code so that it does the following instead.

Set the percentage to 10%. If there are more than 6 people, then let the tip be the expenses times the percentage, otherwise let the tip be zero.

You can check your solution against mine.

Finally, let’s assume the restaurant decides to impose a higher tip of 15% for groups of at least 15 people. In Python we can chain various conditions as follows:

In plain English: if the number of people is larger or equal (>=) to 15, let the percentage be 15%, otherwise if (elif, not else if) it is larger than 6 let the percentage be 10%, otherwise let the percentage be 0%.

Activity 3.3

Test the code with the four borderline cases: 6, 7, 14 and 15 people. You must change the number in line 2 four times, and run the code after each change.

The order in which we write the conditions is important, because the computer checks them from top to bottom and executes only one block, for the first condition that is true. The else block has no condition: it’s a ‘catch all’ in case no condition is true.

Activity 3.4

The following code is as before, except that it handles the first two conditions in a different order. Explain why the code is wrong. Hint: repeat the tests and see what happens.

You will notice that now, with 15 or more people, the bill is 59.4, not 62.1. Since 15 people are more than 6, the first condition is true and the first block (percentage is 10%) is executed. The second block (percentage is 15%) is never executed. The third block (percentage is 0%) is executed when there are 6 or fewer people.

The next section shows how a block of code can be executed multiple times.

Sequence Start Iteration: for-loops