在 Class 方法中从 Class 调用属性,代码错误

Calling Attributes from Class inside of a Class Method, Code Error

我是任何编程语言的 classes 新手,但是目前我正在尝试创建一个程序,该程序将根据输入计算用户的纳税情况。

我遇到的问题是,当我在方法 def get_threshold 中调用属性时,数字没有相加。我已将输入声明为 int 但是当我 运行 方法和数字不相加时它似乎正在生成一个列表。

有人能解释一下我做错了什么吗?

想法是 class 将保存用户的所有收入和养老金缴款详细信息,计算税务状况,程序将能够打印相关税务信息。

我在没有使用 classes 的情况下编写了代码,但是我想将它移到 class 中,以便我可以在需要时为其他客户请求信息。

class Client:

    def __init__(self, name, salary, bonus, intdiv, pensionpercent, pensionlump):
        self.name = name
        self.salary = salary
        self.bonus = bonus
        self.intdiv = intdiv
        self.pensionpercent = pensionpercent / 100
        self.pensionlump = pensionlump

    @classmethod
    def from_input(cls):
        return cls(input('name'), int(input('salary')), int(input('bonus')), int(input('enter interest and divdends for the year')),
                   int(input('enter workplace pension contributions as a %')), int(input('enter total of any lump sum contributions'))
                   )

    def get_threshold(self):

        totalgross = (self.salary + self.bonus + self.intdiv, self.pensionlump)
        print(totalgross)


c = Client.from_input()

c.get_threshold()
Code Returns:
name50000
salary5
bonus5
enter interest and divdends for the year5
enter workplace pension contributions as a %5
enter total of any lump sum contributions0
(15, 0)

Process finished with exit code 0

您正在用逗号在您的方法中创建一个元组

def get_threshold(self):

    totalgross = (self.salary + self.bonus + self.intdiv, self.pensionlump) <-- comma is the cause
    print(totalgross)

如果您需要单个值,则需要删除逗号,请改用 +。或者您的公式要求的任何内容。