Python 2 If, Elif and Else

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

This excersice: https://gist.github.com/450f8942d5103c3428685732c87a8e53

def greater_less_equal_5(answer):
if answer > 5:
return 1
elif answer < 5:
return -1
else:
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!!

2 Likes

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:

2 Likes

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