Simple coding
Simple coding

Start this free course now. Just create an account and sign in. Enrol and complete the course for a free statement of participation or digital badge if available.

3 Selection

Further investigate the Python programming language by learning about conditions. 

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’. Note that in Python the multiplication operator is the asterisk.

Activity 3

Interactive feature not available in single page view (see it in standard view).

Let’s further assume the tip is only automatically added 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, the percentage is 10%, otherwise it’s 0%.

Interactive feature not available in single page view (see it in standard view).

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 4

Change the number of people to 6 and confirm the bill is just the expenses in that case.

The selection instruction ‘if condition: block else: block’ chooses which block to execute as follows. The computer checks the condition after the if. If it is true, the computer then executes the block of code (the indented instructions) belonging to the if part. If the condition is false, the computer executes instead the else block. The indentation is needed to know which instructions belong to which part. Afterwards the computer continues executing the non-indented instructions.

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

Take your learning further

Making the decision to study can be a big step, which is why you'll want a trusted University. The Open University has 50 years’ experience delivering flexible learning and 170,000 students are studying with us right now. Take a look at all Open University courses.

If you are new to University-level study, we offer two introductory routes to our qualifications. You could either choose to start with an Access module, or a module which allows you to count your previous learning towards an Open University qualification. Read our guide on Where to take your learning next for more information.

Not ready for formal University study? Then browse over 1000 free courses on OpenLearn and sign up to our newsletter to hear about new free courses as they are released.

Every year, thousands of students decide to study with The Open University. With over 120 qualifications, we’ve got the right course for you.

Request an Open University prospectus371