简体   繁体   English

为什么python在此类的第二个方法上抛出错误?

[英]Why is is python throwing error at the second method in of this class?

Why is is python throwing error at the second method in of this class? 为什么python在此类的第二个方法上抛出错误?

class Lutany:
    formulaa = 0
    formulab = 0
    def __init__(self,num):
        self.num = num
        self.formulaa = self.formulaA()
        self.formulab = self.formulaB


    def formulaA(self):
        q = 0
        num = self.num
        while num > 0 :
            q += num + (num - 1)
            num = num - 1
        return q
        self.formulab = formulaB()

    def formulaB(self):
        num = self.num
        q = 0
        while num > 0 :
            q = q + (num * num)
            num = num - 1
        return (0.5 * q)


if(__name__ == '__main__'):

    l = Lutany(675)

    p = l.formulaa
    q = l.formulab 

    print " FormunlA returned " , str(p) , "  for 675 "
    print " FormulaB returned " , str(q) , "  for 675 " 

When running I have the following error: 运行时出现以下错误:

~$ python lutany.py
Traceback (most recent call last):
File "lutany.py", line 30, in <module> 
l = Lutany(675) 
File "lutany.py", line 7, in init 
self.formulab = self.formulaB 
AttributeError: Lutany instance has no attribute 'formulaB'

You've indented the block for the formulaB() method too much (although the edit to the question has destroyed evidence of this). 您已经使formulaB()方法的formulaB()块缩进得太多了(尽管对该问题的编辑已破坏了这一点的证据)。 Make sure that it is at the indent level directly beneath the class indent, not that of the previous method. 确保它位于类缩进的正下方的缩进级别,而不是先前方法的缩进级别。

当您说“ formalaA”中的self.formulab = formulaB()时,您是说self.formulab = self.formulaB()吗?

You're missing parentheses here: self.formulab = self.formulaB . 您在此处缺少括号: self.formulab = self.formulaB Should be self.formulab = self.formulaB() . 应该是self.formulab = self.formulaB()

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

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