if, else and elif

Skip to content

This is a machine-translated text that may contain errors!

Viktig!

if, else and elif are some of the most important, perhaps the most important concepts you will learn in programming.

Part 1 - if

Now we will learn our first keyword: if. if is used when you want to check if something has happened, or something meets certain conditions.

Task 1.1a

Use if to check if you are over 18 years old or not.

age = int(input("Your age: "))

if age > 18:
    print("You are over 18 years old!")

Viktig om mellomrom

One thing you might notice here is that on the last line there is a space at the beginning of the line. This is very important to include in the if. You can try to see what happens if you don’t have this space, the code will not work.

Task 1.1b

What happens if you try to enter exactly 18 as the age? Is anything printed now? Let’s fix this now!

There are several ways to compare. Here you can see what exists:

Comparison Result Example
== Exactly equal age == 18 - works for exactly 18
> Greater than age > 18 - works for 19 and up
< Less than age < 18 - works for 17 and down
>= Greater than or equal to age >= 18 - works for 18 and up
<= Less than or equal to age <= 18 - works for 18 and down
!= Not equal age != 18 - anything other than 18

Which comparison must you use here to check if you are 18 or older? Try it out and see what happens!

Svaret:
age = int(input("Your age: "))

if age >= 18:
    print("You are 18 or older!")

Task 1.2a - Compare Text

What if we are going to compare text? Yes, then it is done in exactly the same way!

Use an input() to get an animal from the user, store the value in a variable called animal. Check if the animal that was written out is, for example, cat, then print something if this is correct. You can check text in this way:

dyr = input("Hva er ditt favorittdyr: ")

if dyr == "katt":
    print("Ditt favorittdyr er en katt! *meow*") # Your favorite animal is a cat! *meow*

Task 1.2b

What happens if you try to enter Katt as input, does it work? What about KATT, KaTt, kATT?

Hva skjer?

The most important thing to know is that you must write the text correctly. Uppercase and lowercase letters are different when it comes to code, so KATT and katt are as different from each other as katt and hund.

Part 2 - else

The next keyword we will look at is else. This is a keyword used when what happens in if does not happen. Let’s look at an example:

Task 2.1

Use else to print that is not over 18. We still want the message from task 1.1b to be printed if you are over 18.

Svaret:
age = int(input("Your age: "))

if age >= 18:
    print("You are 18 or older!")
else:
    print("You are not over 18!")

Still pay attention to spaces

It is important that else is on the same indentation as if, or Python won’t know which if the else belongs to! We will explain more about spaces in Python 3.

Task 2.2

Modify the code in task 1.2a to print a message if you do not type “cat”.

Svaret:
dyr = input("What is your favorite animal: ")

if dyr == "katt":
    print("Your favorite animal is a cat! *meow*")
else:
    print("I don't know that animal...")

Part 3 - elif

The last keyword we will look at now is elif. elif is short for else if, and means, as you might guess, otherwise if. If it’s confusing, we can look at an example.

Task 3.1

We will modify the code in task 1.2a to print a similar message for other animals. We can use elif like this:

dyr = input("Hva er ditt favorittdyr: ")

if dyr == "katt":
    print("Ditt favorittdyr er en katt! *meow*") # Your favorite animal is a cat! *meow*
elif dyr == "hund":
    print("Ditt favorittdyr er en hund! *woof*") # Your favorite animal is a dog! *woof*

Task 3.2a

Combine the code from task 2.2 with the code from task 3.1 to print a message if none of the animals match.

Svaret:
dyr = input("What is your favorite animal: ")

if dyr == "katt":
    print("Your favorite animal is a cat! *meow*")
elif dyr == "hund":
    print("Your favorite animal is a dog! *woof*")
else:
    print("I don't know that animal...")

Task 3.2b

Add more elif statements to check for more animals.

Eksempel Svar:
animal = input("What is your favorite animal: ")

if animal == "cat":
    print("Your favorite animal is a cat! *meow*")
elif animal == "dog":
    print("Your favorite animal is a dog! *woof*")
elif animal == "pig":
    print("Your favorite animal is a pig! *oink*")
elif animal == "duck":
    print("Your favorite animal is a duck! *quack*")
elif animal == "elephant":
    print("Your favorite animal is an elephant! *toot*")
else:
    print("I don't know that animal...")

Remember! You can have as many elif statements as you like!