The if and else statement

Screen Link:
https://app.dataquest.io/m/331/python-data-analysis-basics/3/converting-ages-to-decades
My Code:

for age in final_ages:
    if age == "Unknown":
        decade = age
    else:
        age != "Unknown"
        decade = str(age)
        decade = decade[:-1]
        decade = decade + "0s"
    decades.append(decade)
Replace this line with your code

What I expected to happen:
Well, I often try to check answers to class works even when mine is confirmed to be correct.
My code answer was a bit different from the answer code by a line. I thought I should have run into some error but despite the difference, I still got the answer. The line of code I had which wasn’t part of the code in the answer is the first line after the else statement.

What actually happened:

decades = []

for age in final_ages:
    if age == "Unknown":
        decade = age
    else:
        decade = str(age)
        decade = decade[:-1]
        decade = decade + "0s"
    decades.append(decade)

As seen above after the else statement, I included a condition but the answer code skipped that second condition and went directly to converting to string.

@jomedah, welcome to the community!

When you use the if statement, the else statement does not require a conditional statement.

Also, note that since the if-else conditional statement is used when there are only two possible cases, the block of code under the if statement runs when the condition after it evaluates to True; while the block of code under the else statement executes when the condition evaluates to False.

In your block of code, this line of code: age != "Unknown" just evaluates into a boolean value and is not used as a condition for the else statement. Even if you used, age == "Unknown", your code will still run without error. Although both lines of code will be useless there, this is because the block of code under the else statement runs only if the condition after the if statement returns False.

I hope this answers your question.

1 Like

Hi @jomedah. In your else block, like what @doyinsolamiolaoye mentioned, you are not required to give a condition. Only if and elif statements require a condition. In your case there can only be 2 possibilities: age is either Unknown or not Unknown and python works in such a way that if the first condition is not met (i.e. evaluated to False), the python interpreter immediately goes to execute the else block unless you have an elif statement. The general structure of conditions is as follows:

if <condition>: #if statement(compulsory)
   ...
   ...
elif <condition>: #elif statement (optional)
   ...
   ...
else: #else statement(compulsory; sometimes optional)
   ...
   ...

Do note that only ONE of the blocks gets executed.

For example:

age = 50
if age <= 25:
    print("I have not graduated from university")
elif age <= 60:
    print("I am a working adult") #this gets executed
else:
    print("I am an elderly person")

In this example, the if statement is False and thus the interpreter moves on to check the elif statement and execute it since its True (50 is <= 50) and finally skip the else block since the elif block has already been executed.

But in the case where I remove/comment out the elif statement, the else block will be executed automatically as long as the if statement is not True.

age = 50
if age <= 25:
    print("I have not graduated from university")
"""elif age <= 60:
    print("I am a working adult")"""
else:
    print("I am an elderly person") #this gets executed

Hope this helps!

1 Like

Thanks Doyin, I understand you perfectly. I thought I would run into some error given that I added an extra line of code. Thanks again.

1 Like

Oh yes! It helped. Thank you so much.

no worries @jomedah :smile:!