0

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!

RobertJRodriguez
  • 27
  • 1
  • 3
  • 10

4 Answers4

5

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)
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • This is fantastic thank you. Using "return" definitely solved my issue. Thing is, I'm doing this by chapter out of "Starting out with Python" by Tony Gaddis and the book hasn't mentioned using return to send the info back up yet. I suppose there's a more complicated way to do this? – RobertJRodriguez Sep 08 '13 at 20:41
  • You could use globals, but I can't believe it hasn't mentioned `return` values from functions. This is one of the most fundamental parts of programming. – Jonathon Reinhart Sep 08 '13 at 23:08
2

Don't use print in all your functions; let them return values instead:

def total_hourly_cost(rate1, rate2):
    result = rate1 * rate2
    return result

Then you can print the result from main():

print('The total labor charges are: {}'.format(total_hourly_cost(rate_factor, 20)))

But then if you look at your functions, they are all doing the same thing: multiplying two arguments. You don't need multiple functions all doing the same job. In fact, you don't need any functions at all. Ditch the functions and use variables:

total_hourly_cost = rate_factor * 20
print('The total labor charges are: {}'.format(total_hourly_cost))
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
0

You should look at how you can return values from functions in python, store them in variables and then reuse them for other computations.

http://docs.python.org/release/1.5.1p1/tut/functions.html

Ankur Agarwal
  • 23,692
  • 41
  • 137
  • 208
  • Whoa, Python 1.5.1? I'd recommend [more up-to-date documentation](http://docs.python.org/3.0/tutorial/controlflow.html#defining-functions). – icktoofay Sep 29 '13 at 23:24
0

We can multiply multiple arguments, by following ways:

>>> def mul(*args):
    multi = 1
    for i in args:
          multi *=i
    return multi

mul(2,8)

16

mul(2,8,3) 48

Aashutosh jha
  • 552
  • 6
  • 8