Sometimes people write code with if/else statements, but the else statements never execute, regardless of what they type in to be checked in their conditions.
Let us consider two examples of such cases.
I will use Python to illustrate the points.
Scenario 1: if a or b
user_answer = input("Do you want to continue? y/n: ")
if user_answer == "y" or "Y":
print("You chose 'Yes.'")
else:
print("You chose 'No.'")
The output of this code will always be "You chose 'Yes.'"
Why is that? Because "Y" evaluates to True and therefore, the second part of the if condition - the part that says or "Y" - will always be True.
Any non-empty string will evaluate to True.
Also, non-empty containers such as lists, tuples, sets or dictionaries will evaluate to True.
The same applies for non-zero numbers.
You can play around with the following code to test this:
my_variable = "" #no space
if my_variable:
print("It is True")
else:
print("It is False")
my_variable = 1 # 1
if my_variable:
print("1 is True")
else:
print("1 is False")
my_variable = 0 # 0
if my_variable:
print("0 is True")
else:
print("0 is False")
my_variable = [] #empty list
if my_variable:
print("[] is True")
else:
print("[] is False")
my_variable = [0] # list with an element
if my_variable:
print("[0] is True")
else:
print("[0] is False")
So, back to our code. How do we fix it?
The solution is simple: we write our conditions in full, as follows:
if user_answer == "y" or user_answer == "Y":
print("You chose 'Yes.'")
else:
print("You chose 'No.'")
Our program will now check our condition and act as expected.
Scenario 2: if not a or not b
Consider the following piece of code:
user_answer = input("Do you want to continue? y/n: ")
if user_answer != "n" or user_answer != "N":
print("You chose 'Yes.'")
else:
print("You chose 'No.'")
The output of this code will always be "You chose 'Yes.'"
Why is that?
For ease of reference, let us name our conditions as follows:
We will call user_answer != "n" Condition A and we will call user_answer != "N" Condition B.
Now, since we have use the or operator, only one of the two conditions needs to be True for the if statement to evaluate to True.
So if only Condition A is True, then that is enough for the if to be True.
Similarly, if only Condition B is True, then that is enough for the if to be True.
The thing is, whatever user_answer is, at least one of the conditions will be True.
If user_answer is "n" then it is NOT "N" so Condition B is True.
If user_answer is "N" then it is NOT "n" so Condition A is True.
If user_answer is one of the two options, then the other condition is True.
If user_answer is "74" then it is NOT "n" so Condition A is True and Condition B is also True.
So, in the above example, the if statement will always be True and any else or elif statements will never be executed.
How do we fix this? Well, that depends on the actual logic of your program. One simple fix is to replace 'or' with 'and' as follows:
user_answer = input("Do you want to continue? y/n: ")
if user_answer != "n" and user_answer != "N":
print("You chose 'Yes.'")
else:
print("You chose 'No.'")
This means that the if condition will be True only if user_answer is not "n" and it also is not "N"
If user_answer is "n", then Condition A becomes False and hence the whole if statement becomes False (since it uses 'and').
Happy coding!
Top comments (0)