Python Forum
function not giving back total
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function not giving back total
#1
Hello there

New to python, I have a background in java, I'm finding oop in python confusing, this program simply prints out employee info and its suppose calculate their wage, when I run the program it just displays 0, any help appreciated thanks.

class Pay():
    def __init__(self, name, age, hoursWorked, amount_per_hour, total):
        self.name(name)
        self.age(age)
        self.hoursWorked(hoursWorked)
        self.amount_per_hour = amount_per_hour
        self.total = total

    def name(self):
        return self.__name

    def name(self, newname):
        if not newname:
            raise Exception("NAME FIELD CANNOT BE EMPTY")
        self.__name = newname

    def age(self):
        return self.__age

    def age(self, newvalue):
        if newvalue < 18:
            raise Exception("INVALID AGE")
        self.__age = newvalue

    def hoursWorked(self):
        return self.__hoursWorked

    def hoursWorked(self, newhours):
        if newhours < 0:
            raise Exception("HOURS MUST BE ENTERED")
        self.__hoursWorked = newhours

    def amount_per_hour(self):
        return self.__amount_per_hour

    def amount_per_hour(self, newamount):
        self.__amount_per_hour = newamount

    def total(self):
        return self.__total

    def total(self, newtotal):
        self.__total = newtotal

    def calculate_wage(self):
        self.total = self.hoursWorked * self.amount_per_hour
        return self.total

    def __str__(self):
        return "Name: " + self.__name + "\nAge: " + str(self.__age) + "\nHourWorked: " + str(
            self.__hoursWorked) + "\nAmount per Hour: " + str(self.amount_per_hour) + "\nTotal: " + str(self.total)


p = Pay("Kevin", 34, 40, 9.80, 0)
print(p)
The output

Quote:Name: Kevin
Age: 34
HourWorked: 40
Amount per Hour: 9.8
Total: 0

Total field is 0 but I want it to display total wage.
Reply
#2
Python does not support polymorphism based on the signature. Each time you you use the same name for a method it replaces the previous method declaration.
    def total(self):  # This method is thrown away because of the method below.
        return self.__total
 
    def total(self, newtotal):  # This method replaces the total method defined above
        self.__total = newtotal
I think you want to use properties.

https://docs.python.org/3.8/library/func...l#property

The total field is 0 because you initialize total to zero and never change it. I would not make total a variable in class because it can be calculated from hours and hourly wage. If you want to have a total in Pay you'll have to compute total each time hours or pay rate is changed.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Struggling for the past hour to define function and call it back godlyredwall 2 263 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  Get multiple values from function and total it. nirlep 5 1,064 Feb-05-2019, 06:54 AM
Last Post: nirlep

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020