function not giving back total

Associate
Joined
11 Jul 2009
Posts
1,318
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.


Code:
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

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.
 
Thanks visibleman and Th0nt for taking the time to reply.

I would go with something like:

Code:
class Pay():
    def __init__(self, name, age, hoursWorked, amount_per_hour, Subtotal):
        self.name(name)
        self.age(age)
        self.hoursWorked(hoursWorked)
        self.amount_per_hour = amount_per_hour
        self.Subtotal = hoursWorked * amount_per_hour

..and adjust the code to suit where your calling the functions.

So if your working out a wage, your calling the Subtotal with the function that uses the amount of days they have clocked in for.

This worked thanks

I changed it to

Code:
    def __init__(self, name, age, hours_worked, amount_per_hour, Subtotal):
        self.set_name(name)
        self.age = age
        self.hours_worked = hours_worked
        self.amount_per_hour = amount_per_hour
        self.Subtotal = self.calculate_wage()
 
def calculate_wage(self):
        return self.hours_worked * self.amount_per_hour
 
Can anyone tell my total and average aren't being printed out or even calculating by the looks of it

Code:
list = []
total = 0
num = 0

keep_going = True


def get_input():
    while keep_going:
        try:
            num = int(input("Please enter numbers into list or 0 to exit: "))
            if num == 0:
                break
            list.append(num)
        except ValueError:
            print("VALUE MUST BE INTEGER! ")
    sum_array(num)
    calculate_average(num)
    print_answers(num, average)


def sum_array(num):
    for nums in list:
        num += int(nums)


def calculate_average(num):
    global average
    average = num / len(list)


def print_answers(num, average):
    print(list)
    print("Total: ", num)
    print("Average: ", average)


get_input()
 
Back
Top Bottom