--Originally published at TEC GDL 2016
Today I will explain to you when and how to make use of the “else” statement & the use of “elif”.
The else statement is usually combined with an if statement. The coded block following the ‘else’ is only regarded and executed by the program if the previously stated “if” statement (for further information visit my last blog post) resolves a 0 or a FALSE value.
It is built up like this:
if expression:
statement block for TRUE condition
else:
statement block for FALSE condition
Have a look at this flow diagram that visually explains the functioning of if & else:
(Taken from: https://www.tutorialspoint.com/python/python_if_else.htm)
For this post I set myself the challenge to combine a conditional ‘if’ statement with ‘else’ and define this operation as a function, to that theoretically I will be able to use this function once more in the future if I’m in need of it.
I found an exercise online that asked me to design a program that asks the user to enter both his usual wage per hour and the hours he/she had worked in the month before. The program I then created was supposed to calculate the monthly wage including the second option of working overtime, where the worker would be paid twice the usual amount.
(for more exercises visit: http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/ifstatements.html)
Have a look how I solved this problem below:
If you do the math, you will see that the calculations are correct either way (if there is overtime and if there is none). In my opinion this task really helped me to refresh my knowledge on defining functions, as well
make use of the newly acquired knowledge regarding the use of the conditional if & else.There are often also situations where you need to distinguish not only between two options, like in the example used above. This is where the use of ‘elif’ comes into play!
In the straightforward example below, the grade that is obtained in a course would be entered into the system and the output would be the ‘letter’ this grade would result to if we were studying in the US.
Because it is overcomplicating things A LOT and making your whole program look fairly messy (remember the Zen of Python ) the use of ‘elif’ allows you to just code this program like this:
Therefore, the usual syntax how a conditional use of if, combined with elif and else would look like this:
if condition 1:
statement block for True condition 1
elif condition 2:
statement block for True condition 2
.
.
else:
statement block for each condition false
This completes my blog post about the use of ‘else’ and ‘elif’. If you have any questions make sure to comment or write me on Twitter @tecjames.
See y’all!
Peace,
James de Tec
For further help make sure to watch this video:
Sources:
https://docs.python.org/3/tutorial/controlflow.html
https://www.tutorialspoint.com/python/python_if_else.htm
http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/ifstatements.html
https://learnpythonthehardway.org/book/ex30.html