15.Median problems


#1



15


On my compiler (not in codecademy) everything is ok, but here i have some problems:

Oops, try again. median([4, 5, 5, 4]) returned 4 instead of 4.5


returns the median of a row


def median(x):
    x=sorted(x)
    med=0
    if len(x)%2!=0:
        med=x[int((len(x)-1)/2)] 
    else:
        med=(x[int((len(x)/2-1))]+x[int((len(x)/2))])/2
             
    return med


#2

on your computer you might have python3, and codecademy has python2. There is a fundamental difference in this python version, python3:

5 + 4 / 2 = 4.5

in python2 however the number get floored:

5 + 4 / 2 = 4

you can solve this problem by using float() or divide by a float (2 is the same as 2.0, but then you get a floating number as result, not a integer)


#3

Thank you so much! I didn't know that :frowning:


#4

in python3 you can mimic python2 behavior:

5 + 4 // 2 = 4

by using //, anyway, python2 and 3 have some differences, you might want to google them, otherwise you might come across weird errors when working on your computer, the most important one, in python2 you can do:

print "something"

in 3 you have to use brackets:

print("something")

oh, and raw_input no longer exist in 3, it is now input.


#5

I heard about input () between python 2. and python 3.
But did not know that codecademy uses python 2.
And every time ask myself why I do not need any parentheless in codecademy
:sweat_smile: