【问题标题】:Multiplying Functions in PythonPython中的乘法函数
【发布时间】:2013-09-08 19:03:16
【问题描述】:

我正在为我的班级编写一个简短的程序,但我被困在最后一部分。当我运行程序时,一切都正常运行,直到我尝试将两个单独函数的成本相乘以定义另一个函数。我该如何纠正这个问题?

这是完整的代码:

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()

伙计们,我很绝望!

【问题讨论】:

    标签: python function definition multiplication


    【解决方案1】:

    最初的问题是您将total_hourly_costtotal_gal_cost 函数自己传递给total_costtotal_cost 期望数字作为参数,而不是函数。

    真正的问题是您的函数只是打印,而您可能希望它们返回它们计算的值。

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

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

    per_hour = total_hourly_cost(rate_factor, 20)
    

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

    total_cost(per_hour, per_gallon)
    

    【讨论】:

    • 这太棒了,谢谢。使用“return”绝对解决了我的问题。问题是,我是按 Tony Gaddis 的“Python 入门”中的章节来做的,而这本书还没有提到使用 return 来发送信息。我想有更复杂的方法可以做到这一点?
    • 你可以使用全局变量,但我不敢相信它没有提到函数中的 return 值。这是编程最基本的部分之一。
    【解决方案2】:

    不要在所有函数中使用print;让它们返回值:

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

    然后你可以从 main() 打印结果:

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

    但是,如果您查看您的函数,它们都在做同样的事情:将两个参数相乘。您不需要多个功能都做同样的工作。实际上,您根本不需要任何功能。 抛弃函数并使用变量:

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

    【讨论】:

      【解决方案3】:

      您应该了解如何从 python 中的函数返回值,将它们存储在变量中,然后将它们重用于其他计算。

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

      【讨论】:

      【解决方案4】:

      我们可以通过以下方式将多个参数相乘:

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

      mul(2,8)

      16

      mul(2,8,3) 48

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-05
        • 1970-01-01
        相关资源
        最近更新 更多