Sometimes you will change what do you depending on the conditions.

For example: IF you wake up in the morning and it is raining THEN you will take a coat to school OTHERWISE you wont.

IF the day is a Saturday AND the alarm clock goes off THEN you might turn it off and stay in bed OTHERWISE you might get up.

Life is full of decisions that you will make depending on certain conditions, computers are no different.

if-else

For a computer to make decisions based on a condition, you must use an IF statement, it has the following structure:

if condition:
	true
	several instructions that are executed
	if the calcualation evaluates to True
else:
	false
	several instructions that are exectued
	if the condition evaluates to False

Consider the following IF statement:

age = int(input("Enter your age "))
if age >= 18:
	print("You are an adult")
else:
	print("You are still a child")

The IF statement explained:

  • after the if is the condition age >= 18 followed by a colon, this is checking to see if the age variable is more than or equal to 18.
  • after that line is code is the code that will only be run if that condition is True. If it is true it will print on screen You are an adult.
  • the word else then follows followed by a colon. The instructions underneath this are what will be run only if that condition is False. If it is false it will print on screen You are still a child.

if-elif-else

An IF statement with an else will only allow you to check a single condition, however if you have more than one condition to check you can use if..elif..else

Consider the following IF statement:

colour = input("Enter the colour of the traffic light ")
if colour == "Red":
	print("STOP")
elif colour == "Amber":
	print("GET READY TO STOP")
else:
	print("GO")

The IF statement explained:

  • the program first checks to see if the colour entered is Red and if it is will print STOP.
  • if the colour entered isn’t red it will go onto the next condition where the elif is and check if the colour is Amber. If it is then it will print GET READY TO STOP
  • if neither conditions are met it will go to the else part of the code and print GO.

NOTE: It doesn’t need to use elif to see if the colour is Green as if it isn’t Red or Amber it must be Green, therefore you can just use else if there are not other options to consider.

Example program 1 - Capital City

The code for the program below ask the user the capital city of England and display a suitable message depending on whether they enter London.

#asks the user to enter a city and stores it in a variable called city
city = input("What is the capital city of England? ")
#checks if the city variable has London stored in it
if city == "London":
    #displays correct if the condition is true
    print("Correct the capital city of England is London")
else:
    #displays wrong if they have entered something else
    print("Wrong, try again")

When run in IDLE:

Example program 2 - Grade Calculator Program

The code for the program below will ask the user to enter a score on a test and then decide what grade they got.

score = int(input("Enter a score between 0 and 100: "))
if score >= 70:
    print("That test score is a grade A")
elif score >= 60:
    print("That test score is a grade B")
elif score >= 50:
    print("That test score is a grade C")
elif score >= 40:
    print("That test score is a grade D")
else:
    print("That test score is a grade U")

NOTE: When elif is used it will only check the next condition if the previous condition is false.

When run in IDLE:

Example program 3 - Taxi Fare Calculator Program

The code for the program below will calculate the fare for a taxi ride depending on the distance and number of passengers. It should add 50% onto the total if there are more than 5 passengers.

#two inputs asking for the distance and passengers
km = float(input("Enter the distance of the taxi ride in kilometres (KM): "))
passengers = int(input("Enter the number of passengers: "))
#calculation to work out the cost
cost = 3 + ((km - 1) * 2)
#checks to see if the number of passengers is more than 5 so that the additional 50% cost can be added
if passengers > 5:
    #calculates the extra cost if the condition is true
    cost = cost * 1.5
#displays the total cost of the journey
print("The total cost of the journey for", passengers, "passengers is £{:.2f}".format(cost))
Important Points to Note
  • there is no else on this
  • the print code is not indented, this is because it is not part of the if and will display regardless of whether there are more than 5 passengers.
  • in the print command the cost is formatted to currency, this is done by writing                                              £{:.2f}".format(variable)

When run in IDLE: