Median - a little confused?


Hey guys..
so my code is:

but when I run it I get this error:
Oops, try again. median([1]) returned [1] instead of 1

What's causing it to return the result inside the [ ] brackets?


because else will run? which returns num, which contains the sorted list

for odd lists (lists with odd number of items) the length of the list gives the middle index (this even goes for lists with one item):

1 / 2 = 0
3 / 2 = 1
5 / 2 = 2
7 / 2 = 3
9 / 2 = 4

and so on


Am I heading in the right direction?
I still get an error where median([1]) returns 0.5 instead of 1 but I wrote (len(num)/2 + 0.5)


Is there a problem with how I'm returning the result?


I tried a bit of a different approach but essentially I've got the same issue.


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

on line 7, why + 0.5? i tried to tell you, you don't need this:


def median(numbers): 
        num = sorted(numbers)
        x = 0 
        if len(num) % 2 == 0: 
                x = (len(num)/2 + len(num)/2 + 1)) / 2
                x = len(num)/2
        return x

edit: not sure why this doesn't indent when I type it up here


i get an invalid syntax on this line:

x = (len(num)/2 + len(num)/2 + 1)) / 2

because the parentheses aren't good.

okay, now you calculate the middle index here:

x = len(num)/2

now you need to use the middle index to retrieve the middle value of the list


for that first line, I've actually tried with different syntax a number of times but I get it wrong every time. Is it the parenthesis? Because If I want to divide the entire thing by 2 then I need the parenthesis to keep the order of operations that I want.

I'm not sure how to go about retrieving the middle value?


true, but having more ) then ( will mean they don't match. That is a big problem.

you can use square brackets to retrieve values from list based on index, remember?


oh wait.. didn't catch that sorry I thought I had equal ( and )


I'm getting a syntax error on
Why? I'm pretty sure I used it correctly in the above code


ok fixed the else

Oops, try again. median([1]) returned None, did you remember to return the result? - so now I have to return the result wit the help of [ ] ?


you still calculate the indexes, you need to use this indexes to retrieve values:

x = [1,2,3,4]
print x[0]

use 0 as index to retrieve value example


Hey, sorry, I wasn't able to work on the code but I've been looking at it for the past half hour and I've got a question. Why is it len(num)/2-1 below the if statement and not len(num)/2 + 1?

Also I don't understand by what you meant with the last reply. I've tried writing print x[0] but it doesn't give me any response and I've tried a few other return and print statements with num/x/numbers/& actual inputs but none of it is working. Could you explain it a bit further please?

Sorry once again, I know I'm pretty slow at catching on to this stuff. Bear with me please :slight_smile:


because for even lists, len / 2 gives the right middle value. so len / 2 -1 gives left middle value. again, you can do the math for this:

2 / 2 = 1
4 / 2 = 2
6 / 2 = 3

and so on, the first number is list length, divided by two, which gives the right index (remember, lists are zero indexed based, so if list has length of 4, the indexes are: 0, 1, 2 and 3. In other words, 2 is the right middle index)

x[0] was just showing how to retrieve the item at index 0 from list x. It was just a demonstration of concept, not something you should actually use. In your case, the list is num and the index is stored in x variable


oh that makes sense then because I kept thinking that it gives the middle left value first so it was the other way around for me.

Is this what you're referring to:

I've tried this before but it gives me this error: Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5


wait, i am too hasty, let me rephrase that


what you try is a step closer, but it doesn't work. For even lists, you will first need to retrieve the values from the list, add them together and divide them by two.


wait, wait.. so we're talking about this section of the code:
if len(num) % 2 == 0:
x = (len(num)/2 + len(num)/2 -1) / 2