Search code examples
pythonfunctiondefinitionmultiplication

Multiplying Functions in Python


I'm writing a short program for my class and I'm stuck on the last portion. When I run the program, everything functions appropriately up until I get to the end of the code where I try to multiply the cost of two separate functions in order to define another. How can I rectify this?

Here is the code in full:

def main():
    wall_space = float(input('Enter amount of wall space in square feet: '))
    gallon_price = float(input('Enter the cost of paint per gallon: '))
    rate_factor = wall_space / 115
    total_gallons(rate_factor, 1)
    total_labor_cost(rate_factor, 8)
    total_gal_cost(rate_factor, gallon_price)
    total_hourly_cost(rate_factor, 20)
    total_cost(total_hourly_cost, total_gal_cost)
    print()

def total_gallons(rate1, rate2):
    result = rate1 * rate2
    print('The number of gallons of required is: ', result)
    print()

def total_labor_cost(rate1, rate2):
    result = rate1 * rate2
    print('The hours of labor required are: ', result)
    print()

def total_gal_cost(rate1, rate2):
    result = rate1 * rate2
    print('The cost of the paint in total is: ', result)
    print()

def total_hourly_cost(rate1, rate2):
    result = rate1 * rate2
    print('The total labor charges are: ', result)
    print()

def total_cost(rate1, rate2):
    result = rate1 * rate2
    print('This is the total cost of the paint job: ', result)
    print()

main()

I'm desperate here guys!


Solution

  • The initial problem is that you're passing the total_hourly_cost and total_gal_cost functions themselves to total_cost, who is expecting numbers as arguments, not functions.

    The real problem is that your functions are only printing, when you probably want them to return the value they calculated.

    def total_hourly_cost(rate1, rate2):
        result = rate1 * rate2
        print('The total labor charges are: ', result)
        print()
    
        return result
    

    When you call the function, store that result in a variable (just like you did with input)

    per_hour = total_hourly_cost(rate_factor, 20)
    

    Then pass that result to your final function:

    total_cost(per_hour, per_gallon)