Avast ye, this be a machine-translated text an’ may contain errors, aye!
Viktig!
if, else and elif be some o’ the most crucial, aye, mayhap the most crucial concepts ye’ll be learnin’ in the art o’ programmery.
Part 1 - if
Now we shall learn our first keyword: if. if be used when ye be wantin’ to check if somethin’ has come to pass, or somethin’ meets certain conditions.
Task 1.1a
Use if to check if ye be over 18 years o’ age or not.
age = int(input("Alderen din: "))
# Aye, tell me yer age, ye scallywag!
if age > 18:
print("Du er over 18 år!")
Viktig om mellomrom
Aye, a thing ye might notice here be that on the last line, there be a space at the beginnin’ o’ the line. This be mighty important to include in yer if. Ye can try seein’ what happens if ye don’t have this space, the code won’t be workin’.
Task 1.1b
What happens if ye try t’ enter exactly 18 as the age? Does anythin’ be writin’ out now? Let’s be fixin’ this now!
There be several ways t’ compare. Here ye 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 | age >= 18 - works for 18 and up |
<= | Less than or equal | age <= 18 - works for 18 and down |
!= | Not equal | age != 18 - anythin’ but 18 |
Which comparison must ye use here t’ check if ye be 18 or older? Try it out and see what happens!
Svaret:
age = int(input("Yer age, matey: "))
if age >= 18:
print("Ye be eighteen or older, aye!")
Task 1.2a - Compare Text
What if we be comparin’ text, aye? Well, ‘tis done in much the same way!
Use an input() to fetch a beast from the user, store the value in a variable called dyr. Check if the beast that was writ down be, for example, cat, then print somethin’ if ‘tis true. Ye can check text in this manner:
dyr = input("Hva er ditt favorittdyr: ")
if dyr == "katt":
print("Ditt favorittdyr er en katt! *meow*") # Avast! Yer favorite beast be a cat, aye!
Task 1.2b
What be happenin’ if ye be tryin’ to write Katt as input, does it work, aye? What about KATT, KaTt, kATT?
Hva skjer?
The most important thing to know be is that ye must write the text in the proper way, aye. Capital letters and small letters be different when it comes to code, so KATT and katt be as different from each other as katt and hund.
Part 2 - else
The next keyword we be lookin’ at be else. ‘Tis a keyword used when what happens in if don’t happen. Let’s be havin’ a look at an example:
Task 2.1
Use else to print that ye be not over 18. We still want the message as in task 1.1b to be printed if ye be over 18.
Svaret:
age = int(input("Yer age, matey: "))
if age >= 18:
print("Ye be eighteen or older, aye!")
else:
print("Ye be not eighteen, ye scallywag!")
Aye, pay heed to the spaces still!
‘Tis vital that else be aligned with the same indentation as if, or Python won’t know which if the else belongs to! We shall explain more ‘bout spaces in Python 3.
Task 2.2
Alter the code in task 1.2a to spout a message if ye don’t write “cat”.
Svaret:
beast = input("What be yer favorite beast: ")
if beast == "cat":
print("Yer favorite beast be a cat! *meow*")
else:
print("I be not knowin' that beast...")
Part 3 - elif
The last keyword we be lookin’ at now be elif. elif be short for else if, and means, as ye likely can guess, else if. If ‘tis confusin’, we can be takin’ a look at an example.
Task 3.1
We’ll be alterin’ the code in task 1.2a to print a similar message for other beasts. We can use elif for that, like so:
dyr = input("Hva er ditt favorittdyr: ")
if dyr == "katt":
print("Ditt favorittdyr er en katt! *meow*") # Shiver me timbers, a feline be yer favorite!
elif dyr == "hund":
print("Ditt favorittdyr er en hund! *woof*") # Avast ye, a hound be yer heart's desire!
Task 3.2a
Combine the code from task 2.2 with the code from task 3.1 to print a message should none o’ the beasts match, aye.
Svaret:
beast = input("What be yer favorite beast: ")
if beast == "cat":
print("Yer favorite beast be a cat! *meow*")
elif beast == "dog":
print("Yer favorite beast be a dog! *woof*")
else:
print("I be not knowin' that beast...")
Task 3.2b
Add more elif sentences to check for more beasts, aye.
Eksempel Svar:
beast = input("What be yer favorite beastie: ")
if beast == "cat":
print("Yer favorite beastie be a cat! *meow*")
elif beast == "dog":
print("Yer favorite beastie be a dog! *woof*")
elif beast == "pig":
print("Yer favorite beastie be a pig! *oink*")
elif beast == "duck":
print("Yer favorite beastie be a duck! *quack*")
elif beast == "elephant":
print("Yer favorite beastie be an elephant! *toot*")
else:
print("I be not knowin' that beastie...")
Avast! Ye can have as many elifs as ye please!