Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

I’m new to classes in any programming language, however currently I am trying to create a program that will calculate a users tax position based on inputs.

The issue I’m having is that when I call the attributes in method def get_threshold the figures are not adding up. I have declared the input as int but it appears to be generating a list when I run the method and the numbers don’t add up.

Is anybody able to kindly explain what I have done wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The idea will be that the class will hold all of the user’s income and pension contribution details, calculate the tax position and the program will be able to print relevant tax information.

I have written the code without using classes however would like to move it into a class so that I can request information for additional clients if it’s required.

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

>Solution :

You are creating a tuple in your method with the comma

def get_threshold(self):

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

If you want a single value you need to remove the comma, use a + instead. Or whatever your formula calls for.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading