Median(HELP}


#1

I habe been stock at this exercise (Python: Practice Makes perfect: Median)...everything looks good to me. What could be wrong

def median(x):
p = sorted(x)
b = len(p)
if b % 2 == 0:
c = int(b/2)
v = p[0:c]
n = p[c:]
k = v[-1:]
j = n[0]
k.append(j)
w = sum(k)
q = w/2

    #s = int(t/2)
    return q
else:
    c = int(b/2)
    h = p[c]
    return h

#2

If it was good then you wouldn't be stuck. Put your assumptions away and analyze the code. What does the code do, and what do you expect from it? Add a few print statements to log out the values after each line of code.

I see a lot of code that is not necessary, or over the top, which is complicating matters for you. Look for ways to reduce and simplify, and find ways to limit the number of running variables.


#3

it works on python 3 but not on codecademy


#4

Python 3 defaults to floats in integer division. Python 2 returns an integer so the numerator should be declared as a float.

float(a) / 2

#5

thanks so much mtf. it worked


#6

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