简体   繁体   English

在完成python程序时非常需要帮助

[英]In great need of help on finishing python program

The "Component" argument for addCompnent() method is an instance of the component class. addCompnent()方法的“ Component”参数是组件类的实例。 In short, Component has 2 arguments; 简而言之,Component有两个参数。 "Component(self,name,methodCount)" As you see in my code, I added each Component to a list. “ Component(self,name,methodCount)”正如您在我的代码中看到的,我将每个Component添加到列表中。 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. 我需要在validCount()中做的是返回methodCount!= 0的组件数。从我目前的情况来看,我的validCount()一直返回4,我不知道为什么。 I have debugged it and still not seeing where 4 is coming from; 我已经调试了它,但仍然看不到4的来源。 especially when I initialize it to 0. Can you please point out what I am doing wrong? 特别是当我将其初始化为0时。您能指出我做错了什么吗? I have tried counting the Components that have 0 methodCounts and with none 0 methodCounts, but the numbers are not returning correctly either way. 我试过对具有0 methodCounts且没有0 methodCounts的组件进行计数,但是无论哪种方式数字都无法正确返回。 There are three classes in the whole program but here is just the one I'm having troubles with. 整个程序中有3个类,但这只是我遇到的一个类。 (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. 将“ Component”参数大写为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. 读取此代码的方式就像您正在尝试将类类型添加到addedComponents中一样,而不是类的实例。

  3. Is validCounter supposed to be a class variable for Effort or an instance variable? 是否应该将validCounter用作Effort的类变量或实例变量? If it's an instance variable, put its initialization in your init. 如果它是一个实例变量,则将其初始化放入您的init中。 If it's a class variable, refer to it as Effort.validCounter, not self.validCounter. 如果它是一个类变量,则将其称为Effort.validCounter,而不是self.validCounter。 Same for addedComponents and componentCounter. 与addedComponents和componentCounter相同。

  4. Is validCount supposed to increment every call or return the number of addedComponents with methods? 是否validateCount应该增加每个调用的数量或使用方法返回addingComponents的数量? 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. 无论哪种方式,您都可能希望在for循环之前将validCounter初始化为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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