Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBrian Boring
Courses Plus Student 3,904 PointsAnswered correctly, but one thing confuses me
Let me start by saying I have passed this challenge, so I don't need the correct answer, I just am curious about why my first attempt did not work.
This challenge asks to override the len method and change the result so that len is incorrect.
As you can see in my comments, I originally returned super().len() * 2. I qualified it in my IDE (PyCharm) and it was multiplying the length of the list by 2, but still it wouldn't pass in Treehouse.
I spent 30 minutes chasing my tail and trying to see where I went wrong. In a last ditch effort, instead of multiplying it by two, I added 5 to it, and it passed.
Why would that make any difference? The only thing the challenge asked was that it change the length so that it is not accurate to the original length of the list. Any thoughts?
class Liar(list):
def __len__(self):
return super().__len__() + 5
#Originally had return super().__len__() * 2
3 Answers
andren
28,558 PointsIf you use return super().__len__() - 1
you get an error message that len
cannot return a value under 0. That indicates that the list Treehouse passes in contains 0 objects. Since 0 * 2 = 0 your function would not actually be lying about the length of the list.
William Harrison
9,585 PointsI'm not entirely should, but the challenge says ALWAYS return a false value. Treehouse data sets, are crafted to make things difficult. If the data set had a 0 or Null length, your first attempt would return the correct answer.
Brian Boring
Courses Plus Student 3,904 PointsThat's a really good point you both bring up. I'll bet that is it. Thanks to you both. I'll be able to sleep tonight now. ;)
Quinton Dobbs
5,149 PointsQuinton Dobbs
5,149 PointsMy god, it's so simple!