15. Median


#1



15. Median


File "python", line 10
else:
^
SyntaxError: invalid syntax


This should work except I can't find the syntax error.


def median(lst):
    median_answer = 0
    new = sorted(lst)
    if len(new) % 2 ==0:
        median_answer = (new[(len(new)/2)-1] + new[(len(new)/2)]/2.0 
    else:
        median_answer = new[len(new)/2]    
    return median_answer


#2

look at the line above the error message, pay special attention to the parentheses.


#3

In other words, Check your syntax on this line:

median_answer = (new[(len(new)/2)-1] + new[(len(new)/2)]/2.0

#4

Ahh. So to find the syntax error you look at the line above? I was looking at the else line "else:" and was confused. Many thanks.


#5

the thing is parentheses allow us to put the code on multiply lines:

print(
"hello world"
)

So python expected you continue with the code, which you didn't. You introduced else, which it wasn't expecting


#6

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