Median: Syntax error on the ELSE, of all things?


#1

Why am I getting invalid syntax on the ELSE, of all things?

def median(list_of_things):
    new_list=list_of_things.sort()
    if len(new_list)%2==0:
        return ((new_list[(len(new_list)/2)] * new_list[((len(new_list)/2)-1)])/2
    else: 
        return new_list[(len(new_list)/2)]

#2

The error is likely before the else. Your math looks a little bit off, as well.

May I suggest solving it longhand before attempting to refactor into single returns. There is a lot of repetition in this that could be pared down considerably.


#3

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