-4

我正在尝试从 mathsum 方法调用init方法中列出的任何函数,但不断收到如下错误:

“XXX”对象没有“操作员”属性。

 class Math(object):
        "Main class to generate different math sums based on operator and difficulty levels"

        def __init__(self):
            self.operator = [
                addition,
                subtraction,
                multiplication,
                division
                ])

        def addition(self, a, b): return ('addition', '+', a+b)

        def subtraction(self, a, b): return ('subtraction', '-', a-c)

        def mutliplication(self, a, b): return ('multiplication', '*', a*c)

        def division(self, a, b): return ('division', '/', a/c)

        def mathsum(self, difficulty):
            """Function that generates random operator and math sum checks against your answer"""
            print self.operator

请帮忙

4

2 回答 2

2

你错过了几个self.s:

def __init__(self):
    self.operator = [
        self.addition,
        self.subtraction,
        self.multiplication,
        self.division
        ]

另外,请注意您在函数定义中拼错了乘法。

于 2013-06-08T18:32:30.040 回答
0

有了这个:

class MyMath(object):
    """Main class to generate different math sums based on operator and difficulty levels"""

    def __init__(self):
        self.operator = [ self.addition, self.subtraction, self.multiplication, self.division ]

    def addition(self, a, b):
        return ('addition', '+', a+b)

    def subtraction(self, a, b):
        return ('subtraction', '-', a-b)

    def multiplication(self, a, b):
        return ('multiplication', '*', a*b)

    def division(self, a, b):
        return ('division', '/', a/b)

    def mathsum(self, difficulty, a, b):
        """Function that generates random operator and math sum checks against your answer"""
        print self.operator

调用这个:

MyMath().addition(2, 3)

给出了这个:

('addition', '+', 5)

我不确定您在调用顺序和结果方面的期望是什么,但是如果按上述方式声明和调用,这不会产生错误。

于 2013-06-08T19:06:43.097 回答