This is "Practice Makes Perfect" - 15. median:
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))