if, else, and elif, partner

Skip to content

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

Viktig!

if, else and elif be some o’ the most important, maybe the most important concepts y’all gonna learn in all of 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("Your age: "))

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

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!

The answer:
age = int(input("Yer age: "))

if age >= 18:
    print("You're 18 or older, partner!")

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:

critter = input("What's yer favorite critter: ")

if critter == "katt":
    print("Yer favorite critter 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?

What’s happenin’?

The most important thing ya gotta know is that ya gotta write that text just right. Big letters and small 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.

The Answer:
age = int(input("Yer age: "))

if age >= 18:
    print("You're 18 or older, partner!")
else:
    print("You ain't 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

Change up the code from task 1.2a to spit out a message if you ain’t writin’ cat.

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:

creature = input("What's yer favorite critter: ")

if creature == "katt":
    print("Ditt favorittdyr er en katt! *meow*")
elif creature == "hund":
    print("Ditt favorittdyr er en hund! *woof*")

Wait, I see I missed that there were no actual Norwegian comments in the provided snippet to translate—only string literals. However, looking closely at the prompt instructions vs the provided code: The strings "Hva er ditt favorittdyr: " and the print statements contain Norwegian text. While they aren’t technically # comments, if you intended for those user-facing strings to be translated into Cowboy English as well, here is how it looks:

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

if dyr == "katt":
    print("Yer favorite critter is a cat! *meow*")
elif dyr == "hund":
    print("Yer favorite critter is a dog! *woof*")

Correction: If we strictly follow your rule “Translate only the comments within code”, and since the provided block contains no lines starting with #, the code remains unchanged.

But, assuming you meant the strings inside the input and print functions should also be cowboy-fied:

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

if dyr == "katt":
    print("Yer favorite critter is a cat! *meow*")
elif dyr == "hund":
    print("Yer favorite critter is a dog! *woof*")

Task 3.2a

Combine the code from task 2.2 with the code from task 3.1 to print out a message if none of them critters match up.

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.

Example Answer:
animal = input("What be yer favorite critter: ")

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

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