Median


#1

I'm using Python 3 on my local machine to test some things out. My code gives me 4.5 as it should (18 / 4 = 4.5) I've tried several other list of numbers, all give me back the expected results.

However, when I try to convert this over the Python 2, I get 4 OK, I'll try adding a float to the sum(numbers) / index. That comes back as 4.0.

I'm sure I'm missing something quite easy that would make this come back as 4.5, as expected. Any suggestions?


def median(numbers):
	index = len(numbers)
	numbers.sort()
	if index % 2 == 0:
		print(sum(numbers) / index)
	else:
		index =  int((index - 1) / 2)
		print(numbers[index])
		
		
median([4, 5, 5, 4])


#2

If figured out if I add index = float(len(numbers)) then I get past my initial problem.

Now I have another because I didn't follow directions close enough, which I will figure out.


#3

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