15. Median


#1



Hi! I'm sure I'm missing something silly, but getting a syntax error at line 5 - else:
Any ideas what I'm doing wrong?


def median(numbers):
    sort = sorted(numbers)
    if len(sort) % 2 == 0:
        return (sort[len(sort)/2] + (sort[len(sort)/2-1]) / 2.0
    else:
        return sort[len(sort)/2]
   
numbers = [4,5,5,4]

print median(numbers)


#2

Bracket error on line 4.

... + ( ...

#3

Argh..!! Of course..! Thank you :slight_smile:


#4

Recall that the error indicator shows where parsiing ceased due to unexpected token or expression. The error will be somewhere before that point.


#5

That's really helpful, thanks! I got myself stuck on the "else: invalid syntax" message and didn't look properly at the line before that.


#6

Hey you also forgot a big bracket around your addition in total, "order of operation rule" ;* or / is covered before + or -


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.