Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Advanced Objects Frustration

So 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):
        self.value = random.randint(0, value)

    def __len__(self):
        return self.value

1 Answer

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there, Duane Smith! Way to keep at it! :tada: 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 :smiley:

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! :sparkles:

Jennifer 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 :)