Median exercise: Output error that seems to be a codecademy script problem


With even total numbers in list: Oops, try again. median([1]) returned 3.0 instead of 1
With odd total numbers in list: Oops, try again. median([1]) returned 3 instead of 1

I have run into a number of script errors using the codecademy script, so I've taken to writing the code with vim, and then executing it in python3.5 on another computer for much nicer error handling. The following code, regardless of whether I have a list of even numbers out of numerical order or a list of odd numbers outputs the correct answer, but the codecademy script keeps giving me the above error.

Currently, I'm trying to figure out how to format it so the script will take it and I can move on. It should be noted that the output in the codecademy script and interpreter is also correct.

def median(sequence):
    sort = (sorted(x))
    num = int(len(sort)/2)
    if len(sort) % 2 == 0:
        med_even = (sort[num] + (sort[num-1])) / 2.0
        return med_even
        if (len(sort)) % 2 != 0:
            med_odd = (sort[num])
        return med_odd
x = [1, 7, 4, 2]

Interpreter output in codecademy:
[1, 2, 4, 7]

Remote computer output with a different string of numbers:
[2, 6, 8, 10]
Press ENTER or type command to continue

Odd total numbers on codecademy:
[1, 2, 3, 4, 7]

Odd total numbers on remote computer with different list:
[2, 6, 8, 10, 23]
Press ENTER or type command to continue


vim, that is a challenge.

Anyway, codecademy will call the function multiply times with different lists to test if it is working properly, supplying the list as argument on function call.

which means, here:

sort = (sorted(x))

this only works for the fixed list inside your script, you should instead use the function parameter, so that your function works for any lists you pass into the function call as argument


Aha, thank you!

I thought I had removed that, but clearly skimmed over it during the frustration. I'd put in my specific list for testing purposes, then completely forgot that it needed to come out.

And yes, vim is a bit of a challenge, but it's has been nice to work in once I got used to the commands to move around.


but if you wanted multiply lists for testing purposes, why not simply make multiply function calls: (python3 syntax)

# more function calls

that would be simpler?

there is so much more then moving around that you can do in vim, and the advantage is that you can use it over ssh.


I've got multiple lists in my remote server copy already, and working on learning unittest side by side with python. New to Python and programming outside of editing minor things like colour and text formatting, but not to *nix. I used to use nano or ee, depending on which OS I was using, but vim has been better for learning Python.


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