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 trialDuane Smith
1,919 PointsSo I've kept at this challenge and I don't seem to know what I'm getting wrong. When I test my code in VSCode, it works.
It seems that I'm not seeing something in terms of what the question is specifically looking for. I got help from another user who pointed out some things, but I still wasn't able to bring it together. After tinkering with it after a few days, I came up with this code and it seems to work. When I enter the len function after I create a new instance of my class, It gives me the wrong value, which is what I want. So I don't know what I'm doing wrong.
Any help will be greatly appreciated.
Thank You in Advance,
Duane Smith
import random
class Liar(list):
def __init__(self, value, *args, **kwargs):
super().__len__()
self.value = random.randint(0, value)
def __len__(self):
return self.value
1 Answer
Jennifer Nordell
Treehouse TeacherHi there, Duane Smith! Way to keep at it! You're doing great. Part of the problem here is that you don't get to decide what gets passed in upon creating a new instance of Liar()
inside the challenge. The Liar()
class will only accept one argument and that is of type list
.
You do not need to override the __init__
. You only need to override the __len__()
method here. Part of the inherent problem with returning a random value is that you cannot guarantee that the random number that was rolled does not accidentally match the actual length. For example, if I sent in ['a', 'b', 'c']
then that has a len()
that would return 3. But what happens if the random number it rolls is a 3? Then it's not lying
I'm expecting something more along the lines of this pseudocode:
class Liar(list):
def __len__(self):
# length = call super() then call __len__()
# return length + 15
Hope this helps!
Duane Smith
1,919 PointsDuane Smith
1,919 PointsJennifer Nordell
Ahh, I see what you mean! I understand where I wasn't grasping the problem before. Thanks a ton, and thanks for not just giving me the answer as well! You gave me a great blueprint and I definitely have an idea of how I'm going to handle this challenge! Just one more learning lesson of many more to come, love it!
Thanks again :)
Duane