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 trial

Python Object-Oriented Python Advanced Objects Frustration

Bad question: wtf? This is my fourth solution to this problem, and all I get is Bummer: Try again!

As stated above, I have tried several different things, and I just keep getting "Try again!" I am flailing. I think I really don't understand classes and overriding methods. Can I at least get a clue about where to start attacking this problem? Thank you.

frustration.py
class Liar(list):
    super()
    def __new__(*args, **kwargs):
        len = len.__new__(*args, **kwargs)
        return len(list) * 3 / 2 + 2

3 Answers

Cheo R
Cheo R
37,150 Points

Looks like your first method is off by one space (see how the two def don't line up). Other than that it should be passing (you could also do the import random part at before the class definition).

Thank you! Fixing the space didn't pass, but moving import random outside the class definition did.

Cheo R
Cheo R
37,150 Points

You might be over thinking it (which is common). Sometimes we just need to step back, take a break and review what we understand of the problem.

When I don't think I understand the problem, I like to take the prompt and make it into comments. Adding code as I go along. For example:

# make a subclass of list
# Name it Liar.

# Override the __len__ method
# so that it always returns the wrong number of items in the list.

# For example, if a list has 5 members, the Liar class might say it has 8 or 2.

# You'll probably need super() for this.

Then fill in what you know:

# make a subclass of list
# Name it Liar.
class Liar(list):

# Override the __len__ method

# so that it always returns the wrong number of items in the list.
return 1000  # or import random and use it to return a random number

# For example, if a list has 5 members, the Liar class might say it has 8 or 2.

# You'll probably need super() for this.

Then investigate about the parts you don't know.

# make a subclass of list
# Name it Liar.
class Liar(list):

# Override the __len__ method

def __len__(self):
    return random number here

# so that it always returns the wrong number of items in the list.
return 1000  # or import random and use it to return a random number

# For example, if a list has 5 members, the Liar class might say it has 8 or 2.

# You'll probably need super() for this.
# tried using super, tried without super. it works.

I still can't get it to work. Have tried multiple new iterations, and reviewed the video lectures 4 or 5 times. Here is what I have now:

Still have no solution. Here is my latest code, after another set of many revisions and multiple reviews of the lectures: ''' class Liar(list): import random

    def __init__(self, *args, **kwargs):
        super().__init__()

   def __len__(self, *args, **kwargs):
        l = random.randint(1, 100) // 3 + 1
        return l

'''

Thank you. This is a clear approach to breaking down the problem. It is similar to how I approach it, but it is good to write it down like this rather than hold it in my head.