15. Median


My code is giving me an error, but i'm not sure where the fault is.

File "python", line 5
return answer = (new_list[position] + new_list[position-1]) / 2.0
SyntaxError: invalid syntax

def median(x):
    new_list = sorted(x)
    position = len(new_list)/2
    if position % 2 == 0:
        return answer = (new_list[position] + new_list[position-1]) / 2.0
        return answer = new_list[position-0.5]
    return answer


Both lines 5 and 7 have the same error, when you are returning something, it is incorrect syntax to return somethin while it is being assigned, either you give ur variable answer the median value, and then return answer, or you simply return the value directly. Also, you don't need to return answer at the end of ur code if you are returning ur answer in the conditionals. The 2 simplest ways u can correct these errors, without significantly changing ur codet: U can choose 1) to return the values, inside the conditionals, and completely get rid of variable answer. U can do this by deleting the final line of returning answer, and delete the 'answer =' on lines 5 and 7. 2) If u want to have variable answer, assign variable answer those values, without returning them (deleting the return command in the conditionals), and return answer at the end of ur code. I hope this helps.


     return new_list[position]