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


Messages In This Thread
function not giving back total - by SephMon - Aug-25-2020, 11:42 AM
RE: function not giving back total - by deanhystad - Aug-25-2020, 12:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 645 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  The formula in the function is not giving the correct value quest 1 1,237 Mar-30-2022, 03:20 AM
Last Post: quest
  Struggling for the past hour to define function and call it back godlyredwall 2 2,216 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  Get multiple values from function and total it. nirlep 5 3,223 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