Median- returned int instead of float


#1


I cannot understand why I get an int instead of a float when calculating the median when my list has an even number of elements. Can somebody give me a hint?


Replace this line with your code.


#2

as you discovered dividing two integers gives an integer as result (the number gets floored)

we could simply solve this by dividing by a float? you can also use float() if you want


#3

I tried with float() but it returned 4.0 instead of 4.5, but it works with dividing with 2.0. Still, I encounter another error:


#4

if you use float like this:

float(sum / 2)

it won't work, why not? then python first divide the two integers, floors them, and then cast to a float, so you can do:

float(sum) / 2

or:

sum / float(2)

but then you would be better of to do:

sum / 2.0

also, i wouldn't use sum since it is reserved keyword in python (just like list, so wouldn't use that either, you can recognize them by there blue color), rename the variable (for example you could rename sum to total)

if the list has an odd number of items, the length / 2 gives the middle index:

length:  index:
1 / 2     0
3 / 2     1
5 / 2     2
7 / 2     3

the indexes are perfectly in the middle (here we cleverly use that dividing two integers gives an integer as result)


#5

Thanks for explaining. Still, it doesn't work


#6

can you copy paste your code to the forum so i can run it?

I really recommend renaming your variable which are reserved keywords


#7

Hei, I changed the variable. Same problem thought :frowning:

def median(lista):
    sorted(lista)
    print  sorted(lista)
    if len(lista)% 2== 0:
        mid= len(lista)/2
        sum= lista[mid] + lista[mid+1] 
        return  sum / 2.0
    elif len(lista) ==1:
        return lista[0]
    else:
        return lista[len(lista)/2]


#8

run this code:

def median(lista):
    sorted(lista)
    print  sorted(lista)
    print lista
    if len(lista)% 2== 0:
        mid= len(lista)/2
        sum= lista[mid] + lista[mid+1] 
        return  sum / 2.0
    elif len(lista) ==1:
        return lista[0]
    else:
        return lista[len(lista)/2]

print median([6, 8, 12, 2, 23])

it is mostly yours, with one additional print statement.

The sorted list is never stored anywhere, so the values you got where not of a sorted list


#9

I added a new variable called lista_sortata in which I save the sorted list and made the replacements. I really don't know why I have again this error. What am I doing wrong?


#10

if the list has a length of 4, the middle two items are at index 1 and 2. in other words length / 2 and length / 2 -1

-1 not +1


#11

Thank you so much! You were right. Finally is working!


#12

you can leave out your elif if you want, think about. If the list contains one item, the length is one. 1/ 2 = 0, which perfectly matches you need you need.

one other thing, sorted() is a build in function to sort all sorts of things. .sort() is a method specifically for sorting list:

lista.sort()

now a sorted list is stored in lista, this would be better practice


#13

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