I am currently busy with the practice makes perfect lesson of the python course, and I’m stuck in the last lesson, median.
The input of the median script is a list. it needs to be sorted and return the median.
This is my script:
def median(a_list): new_list = sorted(a_list) if len(new_list) % 2 == 0: middle = new_list[int(len(new_list)/2.0)-1:int(len(new_list)/2.0)+1] median = float(sum(middle)/len(middle)) else: median = float(new_list[int(len(new_list)/2)]) return median
But it throws this error:
median([4, 5, 5, 4]) returned 4.0 instead of 4.5
However, when I run this in for example Rodeo (a Python IDE), it simply gives back 4.5.
I was wondering how this could be? Is there a difference in python?