Andrew553
Andrew553

Reputation: 17

Difference between if statements and elif statements in this context?

def is_stylish(pants_colour, shirt_colour):
    """returns a Boolean True or False to indicate whether the given combination is stylish or not"""
    if pants_colour == 'blue' and shirt_colour =='black':
        return True 
    if pants_colour == 'chocolate' and shirt_colour == 'orhre':
        return False
    if pants_colour == 'blue' and shirt_colour == 'ochre':
        return True
    if pants_colour == 'chocolate' and shirt_colour == 'black':
        return False
print(is_stylish('chocolate', 'ochre'))

The result of the above program is None. However, if I change it to 'elif statement', the program runs well. I cannot see the difference between both programs.

Here is my basic understanding of the execution.

For the first program: each 'if statement' will be evaluated and if the condition is met, then the block should be executed; if that is the case then the result is False.

For the second program: if the preceding condition is met then the following statements would be skipped and lead to the result.

def is_stylish(pants_colour, shirt_colour):
    """returns a Boolean True or False to indicate whether the given combination is stylish or not"""
    if pants_colour == 'blue' and shirt_colour =='black':
        return True 
    elif pants_colour == 'chocolate' and shirt_colour == 'orhre':
        return False
    elif pants_colour == 'blue' and shirt_colour == 'ochre':
        return True
    else: 
        return False
print(is_stylish('blue', 'ochre'))

Upvotes: 1

Views: 111

Answers (3)

paxdiablo
paxdiablo

Reputation: 881463

I think your main problem is that you've mis-spelled ochre as orhre in the chocolate checking line, then used chocolate/ochre to test the first one and blue/ochre to test the second one. The first one would also have worked had you used blue.

Fix that little issue and you'll get a non-None result from your first bit of code as well, even with chocolate.

Your second sample is also incapable of returning None because either one of the if/elif statements will fire, or the else will fire. The equivalent in your first sample would be an unconditional return False at the end of the function.

Upvotes: 1

Li Jinyao
Li Jinyao

Reputation: 978

Because you return at each if statement if the condition is true, there will be only one match both for the two of your code.

For the first program: Each if statement will be evaluate in order, if the condition is met, the return code will result in return at that block, and the rest of the code won't be executed. In your case, it will return at line 6, and the rest of the code won't be exectued. If no condition is met, return nothing.

For the second program: If a condition will be met, it acts like the first program. If no condition met, returns a default False

Upvotes: 1

Svetlana Levinsohn
Svetlana Levinsohn

Reputation: 1556

Ex. 1.

For the first program: each if statement will be evaluated and if the condition is met, then the block should be executed; if that is the case then the result should be False.

This statement does not match what your code is actually doing.

print(is_stylish('chocolate', 'ochre')) cannot return False, your olny combinations for False are:

pants_colour == 'chocolate' and shirt_colour == 'orhre'  # !note: 'orhre' is not 'ochre'
pants_colour == 'chocolate' and shirt_colour == 'black'

So none of the conditions are met, function doesn't have any other return, so it returns None.

Ex. 2.

For the second program: if the preceding condition is met then the following statements would be skipped and lead to the result.

This is correct, in your case, it gets into elif pants_colour == 'blue' and shirt_colour == 'ochre' and returns True.

Difference: the major difference between 2 examples, besides what you have already mentioned, is that in the example 2 you have else: return False condition that will cover all the other cases, so if none of the conditions before else are met, your function will return False, not None.

In the 1st example, if none of the conditions are met, it will just go through all the conditions, won't hit any, and will return None.

Upvotes: 1

Related Questions