Why my code does not work ??? Kindly help

def median(numbers):
    sort = sorted(numbers)
    for item in sort: # len(sort)/2
        if len(item) % 2 == 0:
           item1 = len(item)/2
           item2 = len((item)/2)-1
           average = (sort[item1]+sort[item2])/2
             item3 = (len(item)-1) / 2
             average = sort[item3]
    return average


why do you have a loop?

If you add a function call, you can see error messages which can contain useful information.

you would see the following error message:

Traceback (most recent call last):
  File "python", line 16, in <module>
  File "python", line 5, in median
TypeError: object of type 'int' has no len()

lets look at line 5:

if len(item) % 2 == 0:

do we have an int (integer), yes, item. In other words, len() doesn't work for integers.

don't you intent to get the length of the list?


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