What is my wrong in median?


#1

def median(k):
l=sorted(k)
if len(l)> 1:
if len(l) % 2 !=0:
return l[int((len(l)-1)/2)]
elif len(l) %2 ==0:
return (l[int(len(l)/2)] +l[int(len(l)/2 -1)])/2
else:
return l[0]


#2

update**
you need to check your return values!

@bytepro96030
Problem1.

Your if statements only checks if a list have more than one items.
so your else part should be outside of it.

Problem 2.
Your second return value should be float (introduce 2.0 while dividing)


#3

I execute that code in IDLE without any error :sweat:


#4

Ok I have cleared it a bit more..! check that updated post!


#5

You may be using Python 3.0 , CC have Python 2.0
@bytepro96030
In python 3.0 : division operator(/) returns float value (5/2 -- > 2.5)
In python 2.0 : division operator (/) returns integer value[if both numbers are integer] (5/2 -- > 2)
so to get float values you need to have one of the numbers in float form i.e 5 / 2.0 -- > 2.5


#6

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