Median - Practice makes perfect


#1

Please, help! I am stuck (again) in the Median excercise:

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/5?curriculum_id=4f89dab3d788890003000096#

I tried to make all outcome integers by using "int", but the comment still says:
"Oops, try again. median([1]) returned [1] instead of 1'

Can you help me discover what I am not doing the right way in my code:

#I will be using floor and ceil, so I import Math
from math import *
def median(numbers):
    #I sort the numbers
    numbers.sort()
    return numbers
    #I try to find out if the sequence is uneven
    if len(numbers)%2!=0:
        """In case of an uneven sequence, I can take the middle number"""
        w=numbers[len(numbers)/2.0]
        return w
    elif len(numbers)==1:
        """In case of an uneven sequence of 1 number, I can take that number"""
        u = int(numbers[0])
        return u
    else:
        """Here I try to find the number left and right of the middle"""
        x=floor(len(numbers)/2)
        x=int(x-1)
        y=ceil(len(numbers)/2)
        y=int(y-1)
        z=(numbers[x]+numbers[y])/2.0
        z=int(z)
        return z

#2

This will explain the error message. All that is returned in the original list, sorted.


#3

Thank you, mtf!! After some other issues I had to tackle I made it! :tada: Thanks!!!


#4

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