AttributeError: тип объекта не имеет атрибута - PullRequest
0 голосов
/ 28 сентября 2018

Это работающая многоуровневая программа наследования.когда я запускаю его, он говорит: «AttributeError: тип объекта« старты »не имеет атрибута« математика »».Я проверил ассоциацию классов, и они наследуют.Я новичок, так что это действительно поможет мне двигаться вперед.

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()

1 Ответ

0 голосов
/ 28 сентября 2018

Ваш класс 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...