15/15 Median: why isn'y my code working


#1



My Code isnt working

def median(lst):
sorted(lst)
median = 0
while lst:
if len(lst) % 2 == 0:
upper = lst[len(lst) / 2 - 1]
lower = lst[len(lst) / 2]
median = (upper + lower) / 2.0
else:
median += lst[len(lst) / 2]
return median


This is my code. it seems right but it just doesn't seem to get the right answers


Replace this line with your code.


#2

This is not doing what you might expect.

lst.sort()             # in place sort, no copy made

lists = sorted(lst)    # copy made and sorted, original unchanged.

Please help us understand what this loop is doing?

Please explain why we would need to run an accumulator?


#3

wow thanks man it worked


#4

idk all that stuff is useless when i look back on it u could easily take out the accumulator and the loop because they do nothing i rushed this a bit


#5

What's the hurry? I thought this is supposed to be a practice session?


#6

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