If'n, else'n and elif'n

Skip to content

This here’s a machine-translated text that might contain some errors!

Viktig!

if, else and elif are some of the most important, maybe the most important concepts y’all are gonna learn in programmin’.

Part 1 - if

Now we’re gonna learn our first keyword: if. if is used when ya wanna check if somethin’s happened, or somethin’ meets certain conditions.

Task 1.1a

Use if to check if ya be over 18 years o’ age or not.

age = int(input("Alderen din: "))

if age > 18:
    print("Du er over 18 år!") # You are over 18 years!

Viktig om mellomrom

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

Oppgave 1.1b

What happens if ya try to put in exactly 18 as the age? Does somethin’ get printed now? Let’s fix this right up!

There’s a bunch o’ ways to compare things. Here’s what ya got:

Comparison Result Example
== Exactly the same age == 18 - works for exactly 18
> Bigger than age > 18 - works for 19 and up
< Smaller than age < 18 - works for 17 and down
>= Bigger than or equal to age >= 18 - works for 18 and up
<= Smaller than or equal to age <= 18 - works for 18 and down
!= Not the same age != 18 - anythin’ but 18

Which comparison ya gotta use here to check if ya are 18 or older? Give it a try and see what happens!

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

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

Task 1.2a - Comparin’ Text

What if we gotta compare text? Well, it’s done the same way!

Use an input() to get an animal from the user, store the value in a variable called dyr. Check if the animal that was written out is, fer instance, a cat, then write somethin’ out if that’s the case. You can check text like this:

dyr = input("What's yer favorite animal: ")

if dyr == "katt":
    print("Yer favorite animal is a cat! *meow*")

Task 1.2b

What happens if ya try writin’ Katt as yer input, does it work? What about KATT, KaTt, kATT?

Hva skjer?

The most important thing to know is that ya gotta write the text in the right way. 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’re gonna look at is else. This here’s a keyword used when what happens in the if don’t happen. Let’s take a gander at an example:

Task 2.1

Use else to write out that ya ain’t over 18. We still want the message from task 1.1b to be written out if yer 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

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

Task 2.2

Alter the code in task 1.2a to spit out a message if ya don’t type “katt”.

Svaret:
animal = input("What's yer favorite animal: ")

if animal == "cat":
    print("Yer favorite animal is a cat! *meow*")
else:
    print("Don't rightly know that critter...")

Part 3 - elif

The last keyword we’re gonna look at right now is elif. elif is short for else if, and means, as ya might reckon, otherwise if. If it’s a bit confus’n, we can take a look at an example.

Task 3.1

We aim to tinker with the code from task 1.2a to get it to spit out a similar message for other critters. We can do that usin’ elif like so:

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

if dyr == "katt":
    print("Ditt favorittdyr er en katt! *meow*") # What's yer favorite animal is a cat! *meow*
elif dyr == "hund":
    print("Ditt favorittdyr er en hund! *woof*") # What's yer 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:
animal = input("What's yer favorite animal: ")

if animal == "cat":
    print("Yer favorite animal is a cat! *meow*")
elif animal == "dog":
    print("Yer favorite animal is a dog! *woof*")
else:
    print("I ain't familiar with that critter...")

Task 3.2b

Add a few more elif statements to check for more critters.

Eksempel Svar:
animal = input("What's yer favorite animal: ")

if animal == "cat":
    print("Yer favorite animal is a cat! *meow*")
elif animal == "dog":
    print("Yer favorite animal is a dog! *woof*")
elif animal == "pig":
    print("Yer favorite animal is a pig! *oink*")
elif animal == "duck":
    print("Yer favorite animal is a duck! *quack*")
elif animal == "elephant":
    print("Yer favorite animal is an elephant! *toot*")
else:
    print("I ain't familiar with that critter...")

Now listen up! Ya can have as many elifs as yer heart desires!