15/15 Median What is wrong with my code? HELP!


#1

def media(num):
a=sorted(num)
if es_par(len(a))==True:
m=(a[int(len(a)/2)-1]+a[int(len(a)/2)])/2
return m
else:
m=int(len(a)/2)
return a[m]


#2

Hello, @brajean!
I believe you're supposed to sort the list using the function sort() to later try to find the median of it.


#3

yes i changed that but not works

def media(num):
a=sorted(num)
if es_par(len(a))==True:
m=(a[int(len(a)/2)-1]+a[int(len(a)/2)])/2
return m
else:
m=int(len(a)/2)
return a[m]


#4

media([6, 8, 12, 2, 23]) returned 7.0 instead of 8


#5

You're supposed to use the function sort() and that's not the case there.

a=sorted(num)

You should return the list, not some element at position m, if that position exists on the list.

return a[m]

#6

i solved

def is_even(x):
    if x%2==0:
        return True
    else:
        return False
def media(a):
     a=sorted(a)
     if is_even(len(a))==True:
		m=(a[int(len(a)/2)-1]+a[int(len(a)/2)])/2.0
		return m
     else:
		m=int(len(a)/2)
		return a[m]

#7

That actually works, didn't know about sorted().
Will give it a look :slightly_smiling:


#8

for me it is soo confusing please help me to understand what should i do?
here is my code
def median(num):
num = sorted(num)
length = len(num)
for n in num:
if length % 2 == 0:
return (n)/2

    else:
        return (n+1)/2
return num

#9

this (n+1)/2 returns an integer number but you need a float result
try with this (n+1)/2.0


#10

thanks it get solved