Elif with If and Else

The combination of an if and an else are great for making binary choices - ones where there are only two possible responses to the condition. In real-life as well as in programming, we are sometimes encountered with choices that have more than two possible outcomes. A few good examples of this are:

It is possible to make such choices in code by putting many regular if statements one after the other, but the elif is another kind of statement we can attach to an if to support choices with 2+ outcomes.

We can “attach” an elif to an if similarly to how we would attach an else. However, we are allowed to attach however many elifs we needs, and the number depends on how many options we have to choose from. Here is an example:

preCode

if conditionA:
    codeA
elif conditionB:
    codeB
else:
    codeC

moreCode

Python will start at the top, and check if conditionA is true or false. If it is true, it will run codeA, and then skip over the rest of the chain and move on to moreCode. However, if it is false, it will then jump down to conditionB and check that. If true, it will run codeB and then skip the rest. If contitionB is false, it will move on to the else and run codeC. Visually, the flow of this program functions like so:

graph LR classDef blue fill:#cef,stroke:#059; PC[preCode] --> A{if conditionA:} A --> |True| CA[codeA] A --> |False| B{conditionB:} B --> |True| CB[codeB] B --> |False| CC[codeC] CA --> MC[moreCode] CB --> MC CC --> MC class A blue; class B blue; class C blue; class CA blue; class CB blue; class CC blue; class PC blue; class MC blue;

Notice how with a setup of one if, one elif, and one else, we are able to make a decision between three options. Only one of them will be selected (or “run” in the case of programming). If you have a choice between four options instead of three, just add one more elif:

preCode

if conditionA:
    codeA
elif conditionB:
    codeB
elif conditionC:
    codeC
else:
    codeD

moreCode
graph LR classDef blue fill:#cef,stroke:#059; PC[preCode] --> A{if conditionA:} A --> |True| CA[codeA] A --> |False| B{conditionB:} B --> |True| CB[codeB] B --> |False| C{conditionC:} C --> |True| CC[codeC] C --> |False| CD[codeD] CA --> MC[moreCode] CB --> MC CC --> MC CD --> MC class A blue; class B blue; class C blue; class D blue; class CA blue; class CB blue; class CC blue; class CD blue; class PC blue; class MC blue;

You can add as many elifs to an if statement as you want, depending on how many options there are to choose between. It is also possible to write an if with one or more elif and NOT include an else at the end. When used with if and elif, the ending else basically acts as the last-resort case. It will only run if all of the previous conditions evaluated to be false. If you don’t have one at the end, the if-statement will basically just do nothing if all of them evaluated to be false.

An Example: Register for Class

Rather than talk hypothetically, lets go over a concrete example. Say you are studying Spanish as a second language at the University of Awesome. At UofA, the intro sequence of Spanish courses is:

graph LR classDef blue fill:#cef,stroke:#059; S1[Spanish 101] --> S2[Spanish 102] S2 --> S3[Spanish 201] S3 --> S4[Spanish 202] class S1 blue; class S2 blue; class S3 blue; class S4 blue;

However, not every student has this correct sequence memorized. We want to write a program that students can use to help them determine what Spanish class they should sign up for in the next school semester.

The program should:

I recommend that you attempt to write this program on your own first. After you finish, or get as close as you can, you can come back here to compare your solution to mine. You can click on the button below to reveal the answer (or really, one of many possible answers!).



Show Code



The combination of if, elif, and else will come up tons in your future programming career. We need to make choices often when writing computer programs, and this is the primary mechanism by which you will do so in Python.



PyFlo Home Complete Bookmark Next