简体   繁体   中英

In great need of help on finishing python program

The "Component" argument for addCompnent() method is an instance of the component class. In short, Component has 2 arguments; "Component(self,name,methodCount)" As you see in my code, I added each Component to a list. What I need to do in validCount() is return the number of components where the methodCount != 0. From what I currently have my validCount() keeps returning 4 and I have no idea why. I have debugged it and still not seeing where 4 is coming from; especially when I initialize it to 0. Can you please point out what I am doing wrong? I have tried counting the Components that have 0 methodCounts and with none 0 methodCounts, but the numbers are not returning correctly either way. There are three classes in the whole program but here is just the one I'm having troubles with. (If needed I can post full cod):

class Effort(Component):

    addedComponents = []
    componentCounter = 0
    validCounter = 0

    def __init__ (self):
        return None

    def addComponent(self, Component):
        try:
            if (self.addedComponents.count(Component) != 0):
                raise ValueError
            else:
                self.addedComponents.append(Component)
                self.componentCounter = self.componentCounter + 1
                return self.componentCounter 
        except ValueError:
            raise ValueError("Duplicate component")

    def count(self):
        return self.componentCounter


    def validCount(self):
        if (len(self.addedComponents) == 0):
            return self.validCounter
        else:
            for i in self.addedComponents:
                if (i.getMethodCount() == 0):
                    pass
                else:
                    self.validCounter = self.validCounter + 1
            return self.validCounter

A few comments.

  1. Comment your code. Especially when you have mistakes, discerning what your code is supposed to do can be difficult for outsiders.

  2. It's bad form to capitalize the "Component" argument to addComponent. Use capital letters for class names, lower case for parameter names. This code reads like you are trying to add the class type to the addedComponents, not an instance of the class.

  3. Is validCounter supposed to be a class variable for Effort or an instance variable? If it's an instance variable, put its initialization in your init. If it's a class variable, refer to it as Effort.validCounter, not self.validCounter. Same for addedComponents and componentCounter.

  4. Is validCount supposed to increment every call or return the number of addedComponents with methods? Assuming the latter, you don't need an instance variable. Either way, you probably want to re-initialize validCounter to 0 before your for loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM