Python中的乘法函数

Rob*_*uez 0 python function definition multiplication

我正在为我的班级写一个简短的程序而且我被困在最后一部分.当我运行程序时,一切都正常运行,直到我到达代码的末尾,我试图将两个单独函数的成本相乘以定义另一个函数.我怎么能纠正这个?

这是完整的代码:

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()
Run Code Online (Sandbox Code Playgroud)

我在这里绝望!

Jon*_*art 5

最初的问题是,你传递total_hourly_costtotal_gal_cost功能本身total_cost,谁期待号作为参数,而不是函数.

真正的问题是你的功能只是打印,当你可能希望它们返回他们计算的值时.

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

    return result
Run Code Online (Sandbox Code Playgroud)

当你调用函数时,将结果存储在一个变量中(就像你所做的那样input)

per_hour = total_hourly_cost(rate_factor, 20)
Run Code Online (Sandbox Code Playgroud)

然后将结果传递给最终函数:

total_cost(per_hour, per_gallon)
Run Code Online (Sandbox Code Playgroud)