Median. Help please!


#1


I keeps giving me error. cant I use enumerate function?

the error:
Oops, try again. median([1]) returned -0.5 instead of 1

def median(x):
    for index, item in enumerate(x):
        index1=len(x) / 2 
        index2=len(x)/2 - 1
        if len(x) % 2 == 0:
            return (index1 + index2) / 2
        elif len(x) % 2 ==1:
            return (index1 + index2)/2.0


#2

if the list contains a odd number of items, you can just return the middle item (index1), you only need the average of to numbers if the list contains a even number of items


#3

When there is only one element in the sample space, its value is the median.

if len(x) == 1: return x[0]

#4

why make a special condition for a list with one item? The length of the list is then one, so 1/2=0, which is the index of the number you need to have


#5

That makes perfect sense. I only meant to illustrate that the value at index[0] will be the median.


#6

Thanks. you are right I needed "index1" for odd number. then, how do i build else: ?? after elif i am lost. thanks in advance!

def median(x):
    for index, item in enumerate(x):
        index1=len(x) / 2 
        index2=len(x)/2 - 1
        if len(x) % 2 == 0:
            return (index1 + index2) / 2
        elif len(x) % 2 ==1:
            return index1

#7

you don't need an else?


#8

exactly @stetim94 that is what I am trying to figure out. if I use 'else' i don't know what to return( I returned to 'x' but still didn't work) to. if i don't use it I feel like it is not complete. That is why i am lost!


#9

you don't an else, the list either has a odd number or even number of items, so if and else if is enough. So what it is your are struggling with?


#10

thanks for bearing with me. This is my whole code and i still get the following error

Oops, try again. median([1]) returned 0 instead of 1

def median(x):
    for index, item in enumerate(x):
        index1=len(x) / 2 
        index2=len(x)/2 - 1
        if len(x) % 2 == 0:
            return (index1 + index2) / 2
        elif len(x) % 2 ==1:
            return index1

#11

why do you even have a for loop? You can just use the indexes to access the items in the list, no need for the whole for loop. You are making it yourself a lot more complicated with that for loop


#12

honestly @stetim94 only way i can access it is through enumerate but i am lost along the way though. unfortunately, i can't think of anything else. any guidance about how to access index in the list. sorry!

just added this but still says
Oops, try again. median([1]) returned None, did you remember to return the result?

def median(x):
    for index, item in enumerate(x):
        index1 = len(x) / 2
        index2 = len(x) / 2 -1 
        if index == index1 and len(x) % 2 ==0:
            return (index1 + index2) / 2 
        elif index == index2 and len(x) % 2 ==1:
            return index1

#13

what is wrong with simply doing: x[index1]? Feel free to use the enumerate function, but i am not feeling much for figuring out a solution with such a function, it sounds super complicated


#14

@stetim94 sure. I changed it and here it gives a different error!
Oops, try again. median([4, 5, 5, 4]) returned 5 instead of 4.5

def median(x):
    x = sorted(x)
    index1 = len(x) / 2
    index2 = len(x) / 2 - 1
    index1=x[index1]
    index2=x[index2]
    if len(x) % 2 ==0:
        return (index1 + index2) / 2 
    elif len(x) % 2 == 1:
        return index1

#15

i just explained that error here, please read it


#16

@stetim94 worked! thanks patiently walking me through this. Appreciated!