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 trialGuy Larkin
1,662 PointsBad 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.
class Liar(list):
super()
def __new__(*args, **kwargs):
len = len.__new__(*args, **kwargs)
return len(list) * 3 / 2 + 2
3 Answers
Cheo R
37,150 PointsLooks 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).
Cheo R
37,150 PointsYou 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.
Guy Larkin
1,662 PointsI 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
'''
Guy Larkin
1,662 PointsThank 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.
Guy Larkin
1,662 PointsGuy Larkin
1,662 PointsThank you! Fixing the space didn't pass, but moving import random outside the class definition did.