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

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

liar __len__

Hello, the course is Object-Orientated-Python, the section is Advanced-Objects, and the challenge is titled Frustration (aptly!). With the code below:

import random

class Liar(list):
    def __len__(self):
        return super().__len__() * random.randint(1, 100)

I get the error message 'Bummer! Didn't get the wrong len for a Liar instance'.

It consistantly generates the wrong length of an instance of Liar when run locally.

Any help appreciated

James

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

ok, so in the instructions, it stated that for example a list of 5 objects, would be reported by the liar class as having 2, or 8 objects, so it would seem that the challenge expects a specific lie, namely 3 more or less than present, since negative lengths seem inappropriate for most cases, I added a check to make sure this would not be the case. This now passes.

class Liar(list):
    def __len__(self):
        true_length = super().__len__()
        if true_length - 3 < 0:
            return true_length + 3
        else:
            return true_length + random.choice([3, -3])

1 Answer

Steven Parker
Steven Parker
231,275 Points

It doesn't have to be random.

I'm not sure why, but this seems to be a common misinterpretation of this challenge. And the amount of offset is also not important, so any positive value will do.

James J. McCombie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James J. McCombie
Python Web Development Techdegree Graduate 21,199 Points

Hello Steven,

The challenge requires a wrong result when len is called on a class instance, it does not actually state anything beyond that in terms of requirements. It states an example, a list of 5 could be 8 or 2, it does not state it is expecting anything wrong in particular, so it comes as no surprise it is a 'common misinterpreation'

Steven Parker
Steven Parker
231,275 Points

The instructions could be a bit clearer. And they could warn you that negative values won't be accepted.

You might want to submit a bug report using the Support page.