The first part of computer logic that I see people getting tripped up on is if/else and this doesn’t just happen in coding. I most often see it in programmes like Excel.
When I’m working with a user who is usually quite frustrated with their IF statements before they call me, the first thing I get them to do is take their hands away from the keyboard and breakdown what they are trying to do on paper.
Break it down to IF something is true THEN do this, or ELSE do this (IF-THEN-ELSE).
When creating your own programmes it is easy to get confused because you may now be saying IF something is true THEN do this, or ELSE IF this is true THEN do this or ELSE IF this is true… and so on so on.
In python elif is short for ELSE IF.
Continue to keep IF-THEN-ELSE in your head as you work through this and in your own coding and it will help to break it down.
In the Guess The Number game we will be creating, we will be checking if the player’s guess matches the secret number or if it is higher or lower than the secret number, and what to do in each case.
In Python, and most languages and functions I’ve seen, when we are checking if something is true or false, it is referred to as a condition.
The comparison symbols you will be using are:
- == equal to
- != not equal to
- > greater than
- >= greater than or equal to
- < less than
- <= less than or equal to
IF/ELIF/ELSE layout
A simply layout to IF/ELIF/ELSE might be:
if condition1:
do this
elif condition2:
do this
elif condition3:
do this
elif condition4:
do this
else:
do this
Indentation
Note the indentation throughout your code, as this is very important to how Python runs the code and knows what to do and when.
Type up this code and we will go through it and explain it in more detail.
test_result = 61
pass_rate = 60
if test_result < pass_rate:
print("Sorry, you failed")
elif test_result < pass_rate + 5:
print("You passed... just about")
elif test_result < pass_rate + 20:
print("Congratulations! You passed")
else:
print("Wow! You smashed it!")
Again, note the indentation (I know I’m repeating myself here, but that should also highlight the importance of indentation!).
We have created two variables, the test_result and pass_rate.
Our first condition is asking if test_result is less than pass_rate, it is not, so it skips the next indented line and moves onto the next condition.
Now it is asking if test_result is less than pass_rate plus 5, yes it is, so it runs the indented line and prints “You passed… just about”.
Because this line is true, it does not need to run the next conditions or else lines.
Change the test_result to 95 and run the command again, then change test_result to 15 and run again.
After your first IF condition, if you want to add more conditions, you use ELIF. At the end if you have no more ELIFs to enter, you can catch all other situations with ELSE.
The order of conditions
The order of conditions is important.
When Python comes across a true condition, it executes the code and ignores the remaining ELIF/ELSE lines.
If we changed the test_result to be 35 and run the code, it will tell us we failed, which is correct.
But let’s change the order of the first two conditions.
test_result = 35
pass_rate = 60
if test_result < pass_rate + 5:
print("You passed... just about")
elif test_result < pass_rate:
print("Sorry, you failed")
elif test_result < pass_rate + 5:
print("You passed... just about")
elif test_result < pass_rate + 20:
print("Congratulations! You passed")
else:
print("Wow! You smashed it!")
Here, our first condition is true 35 is less than the pass_rate plus 5, so it executes the print command, telling us we passed, and ignores the remaining ELIF/ELSE lines.
When ordering your IF/ELIF/ELSE lines, think about the order, does the condition and order match what you want and would there by any results that could cause an issue?
Bonus Test
Put the conditions back in the right order.
For each condition and print command, can you change the instruction to include the result number? For example to say “Congratulation! You passed with a score of **”.
Common Mistakes
- Forgetting the indentation
- Using = instead of ==
- Putting conditions in the wrong order
- Using quotes around numbers
- Mistyping variable names
= versus ==
When do you use = and when do you use ==?
Think of using = when you are storing something in your variable
secret_number = 7
Use == then when checking or comparing to the variable value
if guess == secret_number
