0

For some reason the getClassiness Function does not work as it is not able to call the helper function getItemClassiness. Is there any reason this might be? Thanks!

class Classy(object):
    def __init__(self):
        self.items = []
    
    def addItem(self, item):
        self.items.append(item)
        
    def getItemClassiness(item):
        if item == "tophat":
            return 2
        if item == "bowtie":
            return 4
        if item == "monocle":
            return 5
        return 0
    
    
    def getClassiness(self):
        total = 0
        for item in self.items:
            x = getItemClassiness(item)
            total += x
        return total

# Test cases

me = Classy()

# Should be 0
print(me.getClassiness())


# Should be 2
me.addItem("tophat")
print(me.getClassiness())

me.addItem("bowtie")
me.addItem("jacket")
me.addItem("monocle")
print(me.getClassiness())
# Should be 11


me.addItem("bowtie\n")
print(me.getClassiness())
# Should be 15

You can use this class to represent how classy someone or something is. "Classy" is interchangable with "fancy". If you add fancy-looking items, you will increase your "classiness". Create a function in "Classy" that takes a string as input and adds it to the "items" list. Another method should calculate the "classiness" value based on the items. The following items have classiness points associated with them: "tophat" = 2 "bowtie" = 4 "monocle" = 5 Everything else has 0 points. Use the test cases below to guide you!

SDC123
  • 45
  • 6
  • Sorry, I misread. There are two problems here: `getItemClassiness` should be a `@staticmethod`, and it needs to be explicitly looked up like `Classy.getItemClassiness` - yes, even within other `Classy` methods. Python does not have "implicit `this`" - hence all the explicit `self` parameters - so other methods of the class are not in the local scope. – Karl Knechtel Jun 01 '22 at 02:25
  • See for example https://stackoverflow.com/questions/136097 and https://stackoverflow.com/questions/68645 . – Karl Knechtel Jun 01 '22 at 02:26

4 Answers4

1
 class Classy(object):
def __init__(self):
    self.items = []

def addItem(self, string):
    self.items.append(string)
    
def getClassiness(self):
    sum = 0
    for item in self.items:
        if (item == "tophat"):
            sum += 2
        elif (item == "bowtie"):
            sum += 4
        elif (item == "monocle"):
            sum += 5
        else:
            sum += 0
    return sum
Bhavya
  • 11
  • 1
0

In line 21 call for a class method is made without using the self keyword.

 x = self.getItemClassiness(item)

Similarly on line 8 in self keyword is required with as parameter for function definition of getItemClassiness

def getItemClassiness(self, item):
  • Thanks! I tried that before but kept getting this error, Im not sure what is wrong? x = self.getItemClassiness(item) TypeError: Classy.getItemClassiness() takes 1 positional argument but 2 were given – SDC123 Jun 01 '22 at 02:29
  • you can also create the function outside the class and use it without referencing to object. – Excited Electron Jun 01 '22 at 02:32
  • Ok, so the x = self.getItemClassiness(item) is correct, however I just needed to add an @staticmethod above the getItemClassiness() method, and now it works! Thank you for your help! – SDC123 Jun 01 '22 at 02:32
  • @SDC123 It helps to think of `self.getItemClassiness(item)` as `Classy.getItemClassiness(self, item)`. – Edward Ji Jun 01 '22 at 02:35
  • Ok, that makes sense. I guess I was just kinda confused as self is not in java. I appreciate the help! – SDC123 Jun 01 '22 at 02:39
0

You should declare getItemClassiness as a static method because it doesn't require a specific instance. Then you can call the function as you would an instance method.

    @staticmethod
    def getItemClassiness(item):
        ...
    
    
    def getClassiness(self):
        ...
        for item in self.items:
            x = self.getItemClassiness(item)

But still it won't give you 15 for the last test case, because "bowtie" != "bowtie\n". If you intend to ignore white space at the start or the end of the string, use str.strip().

Edward Ji
  • 745
  • 8
  • 19
  • Works! Thank you so much. I am used to Java and just started with python, didn't know about the @staticmethod. I appreciate ur help! – SDC123 Jun 01 '22 at 02:32
0

Here is what I did using Static Method. Got the right output in Test Cases.

class Classy(object):
    def __init__(self):
        self.items = []
        
    def addItem(self, item):
        self.items.append(item)
        
    @staticmethod
    def getItemClassiness(item):
        if item == "tophat":
            return 2
        if item == "bowtie":
            return 4
        if item == "monocle":
            return 5
        return 0
    
    
    def getClassiness(self):
        total = 0
        for item in self.items:
            x = self.getItemClassiness(item)
            total += x
        return total
Joar Leth
  • 691
  • 9
  • 27
Vic
  • 1