Not sure I follow


#1

not sure I follow...

Given:
"If the list contains an even number of elements, your function should return the average of the middle two."

I got the error: "Oops, try again. median([4, 5, 5, 4]) returned 5 instead of 4.5"

So... would the average of five and five not be five?

mycode:

def median(seqq):
	length = len(seqq)
	if length % 2 == 0:
		answer = (seqq[(length/2)-1] + seqq[length/2]) / 2
	else:
		nub = length/2
		answer = seqq[nub]
	return answer

Bug in "Median" lesson?
#2

try add 0.5 in the end of line4,though it's cheating


#3

A median is always plotted on a sorted sample space. Samples are grouped in order then the middle of the list returned.

Your code is complete except for one missing declaration... float().

float(seqq[length / 2 - 1] + seqq[length / 2]) / 2

Like always, D.R.Y. applies. We can still clean up this code some more.


#4

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