KrisRedHead
KrisRedHead

Reputation: 17

What do we need to do to fix this equation so that it works in our Python code?

Assignment and code below...

I am assisting my son with his homework. I know coding, but not Python. He has gotten this far and has asked me to jump in to assist and I am stumped. I believe that the equation for the total take-home salary is too long, but I am not sure what to do to help. The code works as is, but when we try to switch the "...will take home $" + str(emp_1.salary) + ", after taxes." with "...will take home $" + str(emp_1.apply_taxes()) + ", after taxes." for both emp_1 and emp_2 we get an error that apply_taxes is not defined. all we need to do is get the equation to work and we will be good. Any suggestions would be greatly appreciated!

Thank you!!

This is the assignment:

  1. Include to class variables that will account for the federal tax rate (0.2) and the state tax rate (0.0314)
  2. Using these variables, add a method to the init method that will deduct BOTH federal and state taxes from the employee salary.
  3. Using proper concatenation, output both the first employee's salary AND what the employee's take home pay will be after taxes are deducted.
  4. Do the same for the second employee.

This is what the output has to look like

This is the code that we have:

class Employee:

  fed_tax = float(.2)
  state_tax = float(.0314)

  def __init__(self, name, salary):
    self.name = name
    self.salary = salary

  def apply_taxes(self):
    self.salary = int(float(self.salary - ((self.salary * float(self.fed_tax)) + (self.salary * float(self.state_tax)))))

emp_1 = Employee("Isaac Soiffer", 50000)
emp_2 = Employee("Jack Fuller", 45000)

print("The employee, " + emp_1.name + ", salary is $" + str(emp_1.salary) + ".")
print("Employee " + emp_1.name + " will take home $" + str(emp_1.salary) + ", after taxes.")

print("The employee, " + emp_2.name + ", salary is $" + str(emp_2.salary) + ".")
print("Employee " + emp_2.name + " will take home $" + str(emp_2.salary) + ", after taxes.")

Upvotes: 0

Views: 68

Answers (1)

E.Serra
E.Serra

Reputation: 1574

you are not calling apply taxes anywhere: Try something like:

class Employee:

   fed_tax = 0.2
   state_tax = 0.0314

   def __init__(self, name, salary):
       self.name = name
       self.salary = salary
       self.post_taxed_salary = self.apply_taxes()

   def apply_taxes(self):
       return int(float(self.salary - ((self.salary * float(self.fed_tax)) + (self.salary * float(self.state_tax)))))

emp_1 = Employee("Isaac Soiffer", 50000)
emp_2 = Employee("Jack Fuller", 45000)
print('employee {} has salary of {} and after taxes {}'.format(emp_1.name, emp_1.salary, emp_1.post_taxed_salary))

Returns: employee Isaac Soiffer has salary of 50000 and after taxes 38430

On a note, because saalary is an attribute, you can make post_taxed_salary a property, e.g.

class Employee:

   fed_tax = 0.2
   state_tax = 0.0314

   def __init__(self, name, salary):
       self.name = name
       self.salary = salary


   @property
   def post_taxed_salary(self):
       return int(float(self.salary - ((self.salary * float(self.fed_tax)) + (self.salary * float(self.state_tax)))))

Should work as well

Upvotes: 3

Related Questions