Making Decisions - Introduction

Your programs so far always carry out the same commands every time the programs are run. Most programs need to be able to carry out different commands – for example in most programs you can choose commands from menus.

This is done with the Python if command.

The Basic if - a Single Result

If the condition temperature<15 is True, Python will carry out the two indented print commands.

  • The sign < means 'less than' or 'fewer than'
  • Python needs you to 'indent' everything belonging to the if.
  • The best way to indent is to press the TAB key just to the left of the letter Q
  • the 'condition' part of an if will work out as either True or False

Please now run the following program a couple of times, first with a low temperature below 15 and then with a temperature above 15.


In [ ]:
temperature = float(input("Please enter the temperature: "))
if temperature<15:
    print("It is too cold.")
    print("Turn up the heating.")

If you've done it right, you should see a message to turn up heating the first time, but no message the second time

if..else - One of Two Results

Using else, if the condition is true, Python will carry out the first block of indented commands. If the condition is false, Python will carry out the second block of indented commands. Only one block will ever be executed

Please now run the following program a couple of times, just like before. This time you should see a new message with a temperature above 15.


In [ ]:
temperature = float(input("Please enter the temperature: "))
if temperature<15:
    print("It is too cold.")
    print("Turn up the heating.")
else:
    print("You can turn the heating off now")

If you've done it right, you should see an appropriate message for your selected temperatures

if..elif..else - One of Three Results

When the first if condition is true, Python will carry out the first block of indented commands. If the condition is false, Python will have a look at the second condition. If that condition is true, Python will carry out the second block of indented commands. If neither of these conditions are true, the last block of commands will be executed. Only one of the three blocks will ever be executed

Please now run the following program a three times, just like before. This time you should see another new message with a temperature above 25.


In [ ]:
temperature = float(input("Please enter the temperature: "))
if temperature<15:
    print("It is too cold.")
    print("Turn up the heating.")
elif temperature<25:
    print("Please turn the heating off")
    print("Please turn the air-con off")
else:
    print("It is too hot.")
    print("Please turn on the air-con")

If you've done it right you should see the following outputs, one at a time:

Please enter the temperature: 12 It is too cold. Turn up the heating.

Please enter the temperature: 20 Please turn the heating off Please turn the air-con off

Please enter the temperature: 30 It is too hot. Please turn on the air-con

if..elif..elif..else - One of Four (or More!) Results

When the first if condition is true, Python will carry out the first block of indented commands. If the condition is false, Python will have a look at the second condition. If that condition is true, Python will carry out the second block of indented commands.
If the second condition is also false, Python will look at the third condition. If the third condition is true, the third block will be executed.
If none of these conditions are true, the very last block of commands will be executed. Only one of the many blocks will ever be executed

Please now run the following program a four times, just like before. This time you should see another new message with a temperature above 35.


In [ ]:
temperature = float(input("Please enter the temperature: "))
if temperature<15:
    print("It is too cold.")
    print("Turn up the heating.")
elif temperature<25:
    print("Please turn the heating off")
    print("Please turn the air-con off")
elif temperature<35:
    print("It is too hot.")
    print("Please turn on the air-con")
else:
    print("HMGW: Holey Moley Global Warming")
    print("Break out the barbecue!")

You can now do question 13