Help Interpreting Error in Median (15) Exercise


#1



This is "Practice Makes Perfect" - 15. median:
https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096#


Hi guys,

This is my first post! I've read through many other user's posts on this subject, but I feel like I'm having a different issue than most. Most people are having float vs. integer related issues, but the error message I am getting is:

"Oops, try again. median([4, 5, 5, 4]) resulted in an error: 'int' object is not iterable"

I don't seem to understand where this is coming from or what the error means. Can anyone give me a heads up?

Thank you so much!


def median(numbers):
    x = sorted(numbers)            # Sort to find median, store as x
    result = 0                     # Initialize variable result
    middle = len(x) / 2            # Set variable middle to median index of x
    if len(x) % 2 == 0:            # Runs if length of x is even
        result += sum(x[middle], x[middle - 1]) / (2.0)
        # Above adds average of two middle indicies of x to variable result
    else:                          # Runs if length of x is not even (odd)
        result += x[middle]        # Adds median index of x to result
    return result                  # Returns median of x (which is sorted(numbers))


#2

Okay, so I have resolved the issue by removing the sum() function from line 6, and instead I have replaced the comma with a + to get what I thought would be the same result.

I'm still not understanding why sum() didn't work and threw the error "'int' object is not iterable." Is anyone able to tell me what I did wrong?

For those interested, here is the fixed code that now works:

def median(numbers):
    x = sorted(numbers)            # Sort to find median, store as x
    result = 0                     # Initialize variable result
    middle = int(len(x) / 2.0)     # Set variable middle to median index of x
    if len(x) % 2 == 0:            # Runs if length of x is even
        result += (x[middle] + x[middle - 1]) / 2.0
        # Above adds average of two middle indicies of x to variable result
    else:                          # Runs if length of x is not even (odd)
        result += x[middle]        # Adds median index of x to result
    return result                  # Returns median of x (which is sorted(numbers))

#3

from the python doc:
Sums start and the items of an iterable from left to right and returns the
total. start defaults to 0. The iterable‘s items are normally numbers,
and the start value is not allowed to be a string.

basically what this means is that sum will loop over a list, to give you the total:

print sum([1,2,3,4,5,6]) # will print 21

this will give you 21, for just adding two items together we just have +.


#4

Thanks! I appreciate the reply. I didn't know we shouldn't use sum() for less than 3 items, but it makes sense.


#5

we can use sum() for less then 3 items:

print sum([5])

which will give you 5. but sum needs something it can loop over (a list for example), it gives you the sum (the total). for just adding numbers just use +

sum() is a built in function, but of course we could also just write it:

def own_sum(x):
    total = 0
    for i in x:
         total += i
    return total

okay, sum() is probably a bit more sophisticated, but i hope it gives you a impression of how sum works.


#6

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