15 - Median! I dont get it how to convert it into a float


#1

Hello together!
I've read other topics as well, but I don't know how to fix this in my code.
I've tried different things to convert, but there is always the error: "TypeError: list indices must be integers, not float"

Here is my code:

def median(numbers):
    result = []
    for i in numbers:
        result.append(i)
    result = sorted(result)


    if len(result) % 2 == 0:
        print "is even."
        num1 = (len(result) / 2) + 1
        num2 = (len(result) / 2) - 1
        num = (num1 + num2) / 2
        return result[num]
    else:
        print "is not even."
        num = len(result)/2
        return result[num]

print median([7, 3, 1, 4])

I've tried:

num1 = float((len(result) / 2) + 1)
num1 = (len(result) / 2.0) + 1
num1 = (len(result) / (2.0)) + 1
num1 = (len(float(result)) / (2.0)) + 1

...
...

I don't understand it. Can you please help me and explain how I have to solve it?
I know that I have to devide float / float to get a float and not just to convert the solution to a float...

Thanks!


#2

The modulus operator (%) always results in an integer value. You can't get a float from it. Try making your floats integers. :slight_smile:


#3

this code:

print "is even."
num1 = (len(result) / 2) + 1
num2 = (len(result) / 2) - 1
num = (num1 + num2) / 2
return result[num]

i am slightly confused by this last line:

return result[num]

num contains your median (well, nearly) so why use num as index? i would just return num

Having said that, say i have the following list after sorting:

[4,4,5,5]

facts about the list:
the list has a length of: 4
the indexes are (keep in mind that list are zero index based): 0, 1, 2 and 3
so the middle two indexes are: 1,2

So how do i calculate the middle indexes using length?

then there is a final problem here:

num = (num1 + num2) / 2

after some math we get the following:

num = 9 / 2

the problem is that dividing an integer by an integer gives an integer as result. Here you have to use a float (2.0)


#4

Ok thanks. I din'nt knew that modulo just returns int.
Trying to write my code on another way!
Thanks


#5

There is only one place where you use the modulo operator (%) , for determining if your list has a odd/even number of items. And you do this correctly. (i disagree with what 825orion said)


#6

Oh...
Of course I have to return just num. The result(num) was because of the for I used before. Forgot to change it.
I'm trying to write another code. Now I see the problem.
Thanks for your help


#7

I hope my explanation helps. If you need more help, post an updated version of your code + error message + new question. Good luck!


#8

Yes I'm trying to fix it later then. I will let you know if it works or not.
Thanks a lot


#9

Hello back!
Now I have a code which access to the right index of the list:

def median(myList):
s = sorted(myList)
l = len(s)
if l % 2 == 0:
print "is even"
index = l / 2
median = (s[index] + s[index] - 1) / 2
return median
else:
print "not even"
index = l / 2
median = s[index]
return median

But I have still the problem with the float and I really don't get it.
I found a code on the Internet which is pretty short and that works. But I would like to know how to fix my code that it works. I'm so stuck at this problem...

Here is the code I've found on the Internet:

def median(L):
L = sorted(L)
n = len(L)
m = n - 1
return (L[n/2] + L[m/2]) / 2.0

Thanks


#10

Hello together!

I found my mistake. I set the -1 on the wrong place! Here is the code:

def median(myList):
s = sorted(myList)
l = len(s)
if l % 2 == 0:
print "is even"
index = l / 2
median = (s[index] + (s[index -1 ])) / 2.0
return median
else:
print "not even"
index = l / 2
median = float(s[index])
return median

Thanks everyone for helping me.
Best


#11