Python Object-Oriented Python Advanced Objects Frustration

Charles Van Way
Charles Van Way
5,814 Points

Frustration is well-named. I've used the following code in an IDE, and it works fine. But not in the question.

import random

class Liar(list):

def __len__(self, things):
    self.things = things
    return len(self.things) + random.randrange(-3,4, 6)
    super().__len__(self, things)
frustration.py
import random

class Liar(list):

    def __len__(self, things):
        self.things = things
        return len(self.things) + random.randrange(-3,4, 6)
        super().__len__(self, things)

3 Answers

Steven Parker
Steven Parker
171,090 Points

I'm not sure how you tested this successfully in your IDE, but I see a few issues:

  • __len__ should not take any arguments other than self
  • __len__ should not be called with any arguments
  • super().__len__() should be the basis for creating a "guaranteed wrong" value
  • anything after a "return" statement is never executed
Charles Van Way
Charles Van Way
5,814 Points

Thanks for suggestions. I'll work on this.

Nicholas Lim
Nicholas Lim
3,649 Points

OK everybody here's the answer:

import random
import math

class Liar(list):
    def __init__(self, *arg):
        self.arg = []
    def __len__(self):
        super().__len__()
        randomNum = random.randint(2, 9)
        x = len(self.arg) + math.factorial(randomNum)
        return x
Steven Parker
Steven Parker
171,090 Points

:warning: FYI: According to a moderator, explicit answers without any explanation are strongly discouraged by Treehouse.