def median(lst): lst = sorted(lst) if len(lst) % 2 == 0: while len(lst) != 2: lst.remove(len(lst)-1) lst.remove(0) integer = (lst(0) + lst(1))/float(2) return integer if len(lst) % 2 != 0: while len(lst) != 1: lst.remove(len(lst)-1) lst.remove(0) return lst Oops, try again. median() returned  instead of 1 I have no idea what the problem is. I double checked my code, and I have found no problems
you return a list, you should return an integer (or float)
you can't use remove(), given you need to end up with a integer/float. You will need to get the values from the list
You seem to be overcomplicating things a little bit. You have also made 2 'if' statements when an 'else' statement would probably be just as good. Here is how I solved it:
def median(numbers): #define a function x = sorted(numbers) #variable for a sorted version of the list y = len(x) #variable for the length of the given list if y % 2 == 0: #if the length is even result = (x[(y / 2)] + x[(y / 2) - 1]) / 2.0 #result is middle 2 numbers divided by 2.0 to get a float else: result = x[y / 2] #if not result is middle number return result #return result
I'm not sure if this is the simplest way to do it, but its rather straight forward. It states that you should not alter the original list in your function which is where I think you have made a mistake. You will need to create a new list, in this case, 'x' and do the maths on that one.
I hope this clears it up a bit for you, ask if you don't understand ill be happy to lend you all the support I can. although a bit of a beginner myself.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.