Reputation: 17
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:
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
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