Python 2 If, Elif and Else

Hello everyone I have a lot of trouble understading this chapter.

This excersice:

def greater_less_equal_5(answer):
if answer > 5:
return 1
elif answer < 5:
return -1
return 0

print greater_less_equal_5(4)
print greater_less_equal_5(5)
print greater_less_equal_5(6)

Sure, it tells me what to do and what to fill but… Why the answers are in this order -1, 0 and 1. Pretty much I don’t understand how this code is written.

If somebody can help me with this, it will let me sleep tonight :slight_smile:

Thank you!!


Firstly if will check if it is true if no then it the elif will check if it is true there can be more elif-s but this will continue till else, which gets called once non of them is true

Was this the problem?
I hope this helps :grinning:


The function foretells of a sorting method where values are given -1 if less than some midpoint value (in this case, 5), +1 if greater than the midpoint, and 0 if equal to the midpoint. However I would not try to read anything into the lesson. Just study and learn from the example.

1 Like