If, else, and eke elif,

Skip to content

This doth be a machine-wrought text which may contain errors!

Viktig!

if, else and elif be some of the most principal, perchance the most principal concepts ye shall learn within the art of programming.

Part 1 - if

Now shall we learn our first keyword: if. if is employed when thou dost desire to examine whether aught hath transpired, or something doth meet certain conditions.

Task 1.1a

Employ if to ascertain whether thou art above 18 years of age, or not.

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

if age > 18:
    print("Du er over 18 år!") # Verily, thou art of age eighteen years and more!

Viktig om mellomrom

Verily, a thing thou mayst observe here is that upon the last line doth reside a space at the line’s commencement. This ‘tis most vital to retain within the if. Thou canst assay to behold what doth transpire shouldst thou lack this space, for the code shall not function.

Oppgave 1.1b

What doth transpire shouldst thou endeavour to input precisely 18 as the age? Doth aught now appear writ? Let us mend this forthwith!

There exist divers ways to compare. Here ye may behold what doth exist:

Sammenligning Resultat Eksempel
== Exact likeness age == 18 - doth function for precisely 18
> Greater than age > 18 - doth function for 19 and upwards
< Lesser than age < 18 - doth function for 17 and downwards
>= Greater than or like age >= 18 - doth function for 18 and upwards
<= Lesser than or like age <= 18 - doth function for 18 and downwards
!= Not like age != 18 - all save 18

Which comparison must thou employ here to check if thou art 18 or older? Essay it and behold what doth occur!

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

if age >= 18:
    print("Du er over 18 eller eldre!")

Task 1.2a - Comparing Text

What if we should compare text? Verily, ‘tis done in the selfsame manner!

Employ an input() to fetch a beast from the user, and store the value in a variable yclept dyr. Examine if the beast which was writ forth be, for example, a cat, and then print somewhat should this be true. Thou mayest check text in this fashion:

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

if dyr == "katt":
    print("Ditt favorittdyr er en katt! *meow*") # Verily, thy favoured beast is a cat! *meow*

Task 1.2b

What doth betide shouldst thou endeavour to inscribe Katt as input, doth it function? What of KATT, KaTt, kATT?

Hva skjer?

That which ‘tis most needful to wit is that thou must inscribe the text aright. Great and small letters differ when ‘tis of code spoken, for KATT and katt are as diverse one from the other as katt and hund.

Part 2 - else

The next keyword we shall observe is else. ‘Tis a word employed when that which doth occur in if doth not come to pass. Let us peruse an example:

Task 2.1

Employ else to print that one is not above 18 years of age. We would still have the message as in task 1.1b writ out shouldst thou be above 18.

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

if age >= 18:
    print("Du er over 18 eller eldre!")
else:
    print("Du er ikke over 18!")

Legg still marke til rom betwixt the lines

Here ‘tis most meet that else doth lie with the self-same indenture as doth if, lest Python know not to which if, the else doth appertain! We shall expound further upon spaces in Python 3.

Task 2.2

Amend the code in task 1.2a so as to print a message shouldst thou not write ‘cat’.

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

if dyr == "katt":
    print("Ditt favorittdyr er en katt! *meow*")
else:
    print("Jeg kjenner ikke til det dyret...")

Part 3 - elif

The last keyword we shall observe anon is elif. elif is short for else if, and doth signify, as perchance thou mayest divine, else if. Should it prove confounding, we may peruse an example.

Task 3.1

We shall amend the code in task 1.2a to print a like message for other beasts. Thus may we employ elif as followeth:

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

if dyr == "katt":
    print("Ditt favorittdyr er en katt! *meow*") # Verily, thy favoured beast is a cat! *meow*
elif dyr == "hund":
    print("Ditt favorittdyr er en hund! *woof*") # Lo, thy favoured beast is a hound! *woof*

Task 3.2a

Conjoin the code from task 2.2 with the code from task 3.1 to print a message should none of the beasts accord.

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

if dyr == "katt":
    print("Ditt favorittdyr er en katt! *meow*")
elif dyr == "hund":
    print("Ditt favorittdyr er en hund! *woof*")
else:
    print("Jeg kjenner ikke til det dyret...")

Task 3.2b

Add yet more elif clauses to assay divers beasts.

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

if dyr == "katt":
    print("Ditt favorittdyr er en katt! *meow*")
elif dyr == "hund":
    print("Ditt favorittdyr er en hund! *woof*")
elif dyr == "gris":
    print("Ditt favorittdyr er en gris! *oink*")
elif dyr == "and":
    print("Ditt favorittdyr er en and! *quack*")
elif dyr == "elefant":
    print("Ditt favorittdyr er en elefant! *toot*")
else:
    print("Jeg kjenner ikke til det dyret...")

Hark! Know ye that as many elif clauses as doth please thee, mayest thou employ!