Program 4: Driving Age Checker

This code asks the user to enter their age. It then tells them whether or not they are aged to drive in the UK. You will see functionality like this often when registering to websites/services.

You can see the code and output below, or you can access it on repl.it by clicking here.

Code

Outputs

New Constructs/Concepts

Selection

This program introduces the concept of ‘selection’. Selection is a way of altering the flow of the program.

So far, we have written programs which run from the first line to the last, in sequence. Sometimes we will want to run the program but only have some of the lines execute.

For example, here we can see that when the age ’27’ was entered, lines 9-11 of the program were skipped. When ’14’ is entered, lines 6-7 is skipped.

This allows us to alter the way that the program behaves, depending on the number entered by the user.

Lines 5 to 11 here are examples of an ‘if’ statement and an ‘else’ statement, which is used to implement selection in Python.

ConstructLine(s)Python SyntaxPurpose
Selection5-11if (condition):
code
else:
code
To perform an addition, minus, division, multiplication, exponent

Mathematical Comparison Operators

To build an if statement we need a boolean condition. We can think of a boolean condition as a test that needs to be passed in order for a line or multiple lines of code to run.

To build a boolean condition, we use mathematical comparison operators:

SymbolMeaning
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

In this example, we are checking if the age entered by the user is greater than or equal to (>=) 17. This is an example of comparing the contents of a variable (age) to a value (17). However, there will be times when we need to compare the contents of two different variables.

ConstructLine(s)Python SyntaxPurpose
Comparison Operator5==, !=, >, <. >=, <=
eg:
age == 18
name == “Bob”
school != “Dumfries”
age >= 17
points <= 100
To compare the contents of a variable to a value, or the contents of two variables.

Code Blocks

An if statement is the first example that we have seen which uses a ‘code block’. This is when multiple lines of code are part of the same statement. A code block has a first line which introduces the statement. It then has a comma followed by a number of indented lines of code.

Lines 6 and 7 are indented here, because they are part of the if statement introduced on line 5. Lines 9-11 are indented as they are part of the else statement. This is really important so that the computer knows which lines to skip for each scenario.

To indent, we use the ‘tab’ key on the keyboard. Most editing environments will automatically indent once you have typed the first line of the if statement (as long as you have included a colon).

Parson’s Problem

This program is designed to check that users that are signing up to a website are at least 14 years old.

Report a Glow concern
Cookie policy  Privacy policy

Glow Blogs uses cookies to enhance your experience on our service. By using this service or closing this message you consent to our use of those cookies. Please read our Cookie Policy.