Esteban
Esteban

Reputation: 33

Python 3.0 Calling a value from a class function to print

Trying to call the total hours from the class/function down to the instance of the class I created below. (hope i worded that correctly)

class Employee:
  def __init__(self):
    self.wage = 0
    self.hours = 0
    if self.hours > 40:
      self.ot = self.hours - 40                 #overtime hours
      self.earned_ot = self.wage * 1.5          #overtime wage rate
      self.ot_total = self.ot * self.earned_ot  #pay for overtime hours
      self.pay = self.wage * 40                 #standard hour wage
      self.total = self.pay + self.ot_total     #standard wage + ot hour wage
    else:
      self.total = self.wage * self.hours

alice = Employee()
alice.wage = 20
alice.hours = 40
#alice.total = 

bob = Employee()
bob.wage = 15
bob.hours = 50
#bob.total = 

print("Alice: \n Net pay: ${} \nBob: \n Net pay: ${}" .format(alice.total, 
bob.total))

Issue I'm having seems simple enough but I can't for the life of me figure it out. I commented out the alice.total and bob.total for now, but those are what I need to get the print to work correctly.

I feel like i'm forgetting something simple, but browsing back through my notes/texts I can't figure it out. Does anyone who sees the issue that I'm having, have a link to something that might explain this? I'm not looking for an outright answer, just a point in the right direction.

Thanks.

Upvotes: 0

Views: 252

Answers (3)

olricson
olricson

Reputation: 311

You could either change your constructor

class Employee:
    def init(self, wage=0, hours=0):
    self.wage = wage
    self.hours = hours

And instantiate your employee

alice = Employee(20, 40)

The rest of your code can be left unchanged. However I'll advise to use something like properties so you can change wage/hours without creating a new Employee and still get a accurate value

class Employee:
     def init(self):
         self._wage = 0
         self._hours = 0

     @property
     def wage(self):
         return self._wage

     @wage.setter
     def wage(self, value):
         self._wage = value

     @property
     def hours(self):
         return self._hours

     @hours.setter
     def hours(self, value):
         self._hours = value

     @property
     def total(self):
         if self.hours > 40:
             self.ot = self.hours - 40                 #overtime hours
             self.earned_ot = self.wage * 1.5          #overtime wage rate
             self.ot_total = self.ot * self.earned_ot  #pay for overtime hours
             self.pay = self.wage * 40                 #standard hour wage
             return self.pay + self.ot_total     #standard wage + ot hour wage
         else:
             return self.wage * self.hours

Your code to instantiate and get/set wage/hours on an Employee can stay the same More details on property : https://www.programiz.com/python-programming/property

Edit: Modified my answer based on comments made

Upvotes: 0

Kos
Kos

Reputation: 72271

Looks like your code would benefit from changing the constructor to:

class Employee:
    def __init__(self, wage=0, hours=0):
        self.wage = wage
        self.hours = hours

and placing the rest of the code in a method:

class Employee:
    ....

    def get_total(self):
        total = ... # calculate from self.wage and self.hours
        return total

alice = Employee(wage=20, hours=40)
print(alice.get_total())

Upvotes: 0

Arthur.V
Arthur.V

Reputation: 706

I think what you're looking for is the @property decorator:

class Employee:
  def __init__(self):
    self.wage = 0
    self.hours = 0

  @property
  def total(self):
    if self.hours > 40:
      ot = self.hours - 40                 #overtime hours
      earned_ot = self.wage * 1.5          #overtime wage rate
      ot_total = ot * earned_ot  #pay for overtime hours
      pay = self.wage * 40                 #standard hour wage
      return pay + ot_total     #standard wage + ot hour wage
    else:
      return self.wage * self.hours

Upvotes: 2

Related Questions