15/15 median


Hi, i don't know why is not updating the variable med after the sum. Here is my code:

def median(l):
....med=0 #ignore the dots

if len(l) == 0:
    med += l[0]
elif len(l) % 2 == 0 and len(l) > 0:
    med += l[len(l)/2]
elif len(l) % 2 != 0 and len(l) > 0:
    med += float(len(l)/2)
return med

print median([1])


I suggest that you add print statements in your code which explain to the reader what the code is doing in plain English.

Then you can read these messages and see where they go wrong, and then fix the part of the code that's responsible for that behaviour.

You might also find some mistakes simply by reading your code, because there are a couple of things that just don't add up. Print statements make these things easier to find though, so it's not like you have to stare your code down and find all your bugs that way.

For example, one thing that you can print out is the result of your conditions. This lets you tell if the conditions are correctly identifying what should be done.
So you'd print like:

Is [1]'s length 0 ? False

Where the [1] is the argument and False is what your condition evaluated to.

For finding why it isn't updating, you would want to find out if any of your conditions turn true so that their bodies are executed. Either they aren't, and that's the problem, or they are, but nothing is being done in them and then you would need to look closer at that