AttributeError:类型对象没有属性

吉姆·比姆(Jim Beam)

这是一个有效的多级继承程序。当我运行它时,它说“ AttributeError:类型对象'starts'没有属性'math'”。我检查了类的关联,并且它们继承了。我是一个初学者,因此它将对我的前进有很大帮助。

class starts:

    def __init__(self, ans, a, b):

        self.ans = input("Please type the operation to do the function as below \n 1. Sum \n 2. Subtract \n 3. multiply \n 4. divide \n")
        self.a = int(input("please enter the number you want to do the operation with : "))
        self.b = int(input("please enter the number you want to do the operation with : "))


class maths(starts):
    def __init__(self, sum, subtract, divide, multiply):

        self.sum = sum
        self.subtract = subtract
        self.divide = divide
        self.multiply = multiply

        def sum(self, a, b):
            print (self.a + self.b)
    #
        def subtract(self, a, b):
            print(self.a - self.b)
    #
        def divide(self, a, b):
            print(self.a / self.b)
    #
        def multiply(self, a, b):
            print(self.a * self.b)


class operations(maths):

    def __init__(self, class_a):

        #super(operations,self).__init__(self.ans, self.a, self.b)
        super().__init__(self.ans, self.a, self.b)

        self.ans = class_a.ans

        if class_a.ans == self.sum:
            print(starts.maths.sum(self.a, self.b))

        elif class_a.ans == self.subtract:
            print(starts.maths.subtract(self.a, self.b))

        elif class_a.ans == self.divide:
            print(starts.maths.divide(self.a, self.b))

        else:
            class_a.ans == self.multiply
            print(starts.maths.multiply(self.a, self.b))


starts.maths.operations()
吹牛

您的operations类继承了maths该类,该类又继承了starts该类,因此,__init__如果您简单地调用,则由父类方法初始化的所有实例变量都可用于子类super().__init__()

class starts:

    def __init__(self):

        self.ans = input("Please type the operation to do the function as below \n 1. Sum \n 2. Subtract \n 3. multiply \n 4. divide \n")
        self.a = int(input("please enter the number you want to do the operation with : "))
        self.b = int(input("please enter the number you want to do the operation with : "))


class maths(starts):
    def __init__(self):
        super().__init__()

    def sum(self, a, b):
        return (self.a + self.b)

    def subtract(self, a, b):
        return(self.a - self.b)

    def divide(self, a, b):
        return(self.a / self.b)

    def multiply(self, a, b):
        return(self.a * self.b)


class operations(maths):

    def __init__(self):
        super().__init__()

        if self.ans == 'sum':
            print(self.sum(self.a, self.b))

        elif self.ans == 'subtract':
            print(self.subtract(self.a, self.b))

        elif self.ans == 'divide':
            print(self.divide(self.a, self.b))

        elif self.ans == 'multiply':
            print(self.multiply(self.a, self.b))

        else:
            print('Unknown operation: %s' % self.ans)

operations()

样本输入和输出:

Please type the operation to do the function as below 
 1. Sum 
 2. Subtract 
 3. multiply 
 4. divide 
sum
please enter the number you want to do the operation with : 3
please enter the number you want to do the operation with : 7
10

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

AttributeError:类型对象没有属性(*args 有问题?)

AttributeError: 类型对象“DirectView”没有属性“as_view”

DRF AttributeError类型对象'QuerySet'没有属性'nom'

AttributeError: 类型对象“DataFrame”没有属性“pd”

AttributeError: 类型对象“页面”没有属性“查询”

AttributeError类型的对象Person没有属性ID

AttributeError:类型对象“用户”没有属性“名称”

AttributeError:类型对象“ Project”没有属性“ _meta”

AttributeError:类型对象“ Car”没有属性“ speed”

AttributeError:类型对象“ MyUser”没有属性“ USERNAME_FIELD”

AttributeError:类型对象“ _io.StringIO”没有属性“ StringIO”

Django:AttributeError:类型对象“ Position”没有属性“ _meta”

Python AttributeError:类型对象“ x”没有属性“ x”

AttributeError:类型对象“ Image”没有属性“ open”

PySpark AttributeError:类型对象“ ALS”没有属性“ trainImplicit”

AttributeError:类型对象“ projectile”没有属性“ dir”

/ api / test类型对象'Product'的AttributeError没有属性'objects'

AttributeError: 类型对象“PrecisionRecallDisplay”没有属性“from_estimator”

Django:AttributeError:类型对象“ MyAppConfig”没有属性“ rpartition”

AttributeError: 类型对象“BST”没有属性“包含”

AttributeError: 类型对象“节点”没有属性“值”

AttributeError:类型对象'FilePath'没有属性'_flavour' [pydantic]

AttributeError:类型对象“fft”没有属性“fft_vcc”

AttributeError: '' 对象没有属性 ''

AttributeError: '...' 对象没有属性 '...'

AttributeError 对象没有属性

AttributeError:“列表”对象没有属性“对象”

flask-sqlalchemy:AttributeError:类型对象没有属性'query',在ipython中有效

AttributeError:“系列”对象没有属性“重塑”