TypeError:不支持*=:“int”和“instance”的操作数类型

2024-06-28 15:49:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道该怎么问,但我的代码是:

class Bruch:

    def __init__ (self,zaehler,nenner):
        self.__zaehler=zaehler
        self.__nenner=nenner

    def __mul__ (self, other):
        self.___zaehler *= other
        self.__nenner *= other

    def mal (self,other):
        self.__zaehler *= other
        self.__nenner *= other

    def __str__(self):
        return "Bruch : " + (self.__zaehler) + "/" + str(self.__nenner)


if __name__ == "__main__":
    bruch1 = Bruch(2,3)
    bruch2 = Bruch(4,5)
    bruchMul = bruch1.mal(bruch2)
    print bruchMul
    bruchMul2 = bruch1*bruch2
    print bruchMul2

当我运行它时,错误:

TypeError: unsupported operand type(s) for *=: 'int' and 'instance'

出现。在

谁能帮帮我吗。在


Tags: 代码selfdefclassmalotherprintstr
1条回答
网友
1楼 · 发布于 2024-06-28 15:49:09

改变功能:

def __mul__ (self, other):
    self.__zaehler *= other
    self.__nenner *= other

收件人:

^{2}$

您希望返回一个新的Bruch而不更改当前的Bruch。在

相关问题 更多 >