Median


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096#


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


Really new at python and have no much clue what is going on. Would really appreciate any help with what went wrong with the code. Thanks!!


def median(chunk):
    refined = sorted(chunk)
    mid_point = len(refined) / 2
    if mid_point % 2 == 0:
        return ((refined[mid_point - 1] + (refined[mid_point]) / 2.0)
    else:
        return (refined[(mid_point + 1) / 2.0])


#2

maybe misplaced round brackets ??

( ( refined[mid_point - 1] + refined[mid_point]) / 2.0)


#3

You don't need the parentheses around the return statement. Remove them and you should be fine.


#4

def median(a):
    b = sorted(a)
    if len(b) % 2 == 0:
        return ( b[len(b)/2 - 1] + b[len(b)/2] ) / 2.0
    else :
        return b[(len(b) - 1) / 2]
        
print median(a)

#5

Try This.

def mediana (l):
    l= sorted(l)
    if len(l)% 2==0:
        return (l[len(l)/2-1] + l[len(l)/2])/2.0
    else:
        return l[len(l)//2]

print mediana([1,1,2])

Saludos desde PerĂº


#6

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