Median: 'list' object has no attribute 'len' . . . HELP!


<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>
Error: (with [1] supplied)
Oops, try again.
median([1]) resulted in an error: ‘list’ object has no attribute ‘len’

<What do you expect to happen instead?>
I need help here obviously. It keeps erroring out. With median[], it kept returning 4 instead of 4.5. And with median[1], I keep getting the one I mentioned above: median([1]) resulted in an error: ‘list’ object has no attribute ‘len’

Any ideas?


def median(x):
new_lst = x
s = new_lst.sort() #sortiing list
leng = new_lst.len(s) #getting length
#Below: was getting an error when the list was just a [1] so made the ‘if’ and ‘elif’ to check if length is greater than 1 and then the other half of the argument.
if leng > 1 and leng % 2 != 0:
med = (leng / 2.0)
return med
elif leng > 1 and leng % 2 == 0:
a = (leng / 2.0)
b = a - 1
c = s.index[a]
d = s.index[b]
med = s[c] + s[d] / 2.0
return med
return s

<do not remove the three backticks above>

Thanks in advance for your help!!!


new_lst = x

will not make a copy of the list. use list() to make a copy:

new_list = list(x)

this doesn’t work either:

s = new_lst.sort()

.sort() is a built in function, it returns the sorted list in the same variable:


then you use len really weird, if you want the length of something you just use len:

x = len([1,2,3,4])

no need for the dot notation.

Good, now you fiddle with it, if you need more help post an updated version of your code


Thank you!

Ugh I know len() for length, duh. I can’t believe I did that. I think it’s because I had new_lst.count() in there, realized that’s not what I needed and just replaced the dot count with a dot len. Stupid LOL!

So with new_lst.sort() will just sort the new_lst and I don’t need an argument?

And for the copy: I thought that that is how you make a working copy so as to not screw up the original list…

I’ll work on these and might be back. Thanks for your help!!!

yes, exactly.

Nope, that is not the case. You can make variable which refer to the same list

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