【问题标题】:How to multiply a function by an integer?如何将函数乘以整数?
【发布时间】:2017-12-01 00:53:29
【问题描述】:

所以我需要计算用户每月花费多少钱,然后使用该数字来计算他们每年花费多少。为此,我想将他们的每月支出乘以 12 以获得他们的年度支出,但我一直遇到同样的错误,即我无法将 int 和函数与运算符“*”相乘

def loan_payment():
    loan = float(input("Please enter how much you expend monthly on loan payments:"))
    return loan
def insurance_cost():
    insurance = float(input("Please enter how much you expend monthly on insurance:"))
    return insurance
def gas_cost():
    gas = float(input("Please enter how much you expend monthly on gas:"))
    return gas
def maitanence_cost():
    maitanence = float(input("Please enter how much you expend monthly on maintanence:"))
    return maitanence
def monthly_cost():
    monthly_expenses = float(loan + insurance + gas + maintanence)
    return float(monthly_expenses)
    print("You expend $"+format(monthly_cost, '.2f')+"in a month.")
def yearly_cost():
    yearly_expenses = 12 * monthly_cost
    return yearly_expenses
    print("At your current monthly expenses, in a year you will have paid $"+format(yearly_cost, '.2f')+".")
def main():
    loan_payment()
    insurance_cost()
    gas_cost()
    maitanence_cost()
    monthly_cost
    yearly_cost()
main()

【问题讨论】:

  • 应该12 * monthly_cost12 * monthly_cost()
  • 你不能将函数乘以整数。也许你将函数调用的结果乘以整数。

标签: python function multiplication


【解决方案1】:

在我看来,您正在学习如何使用函数。

def loan_payment():
    loan = float(input("Please enter how much you expend monthly on loan payments:"))
    return loan
def insurance_cost():
    insurance = float(input("Please enter how much you expend monthly on insurance:"))
    return insurance
def gas_cost():
    gas = float(input("Please enter how much you expend monthly on gas:"))
    return gas
def maitanence_cost():
    maitanence = float(input("Please enter how much you expend monthly on maintanence:"))
    return maitanence
def monthly_cost(loan, insurance, gas, maitanence):
    monthly_expenses = float(loan + insurance + gas + maitanence)

    print("You expend $"+format(monthly_expenses, '.2f')+"in a month.")
    return float(monthly_expenses)
def yearly_cost(monthly_cost):
    yearly_expenses = 12 * monthly_cost

    print("At your current monthly expenses, in a year you will have paid $".format(yearly_expenses, '.2f') + ".")
    return yearly_expenses

loan = loan_payment()
insurance = insurance_cost()
gas = gas_cost()
maitanence = maitanence_cost()

monthly_cost = monthly_cost(loan, insurance, gas, maitanence)

yearly_cost(monthly_cost)

是你想要的。

无论您使用return 语句返回什么,我都将它分配给一个变量。因此,一旦我收集了它们,我就可以将它们传递给收集 4 个变量的 monthly_cost() 函数。

此外,return 仅当您想退出该函数时。如果return 在到达print 语句之前,print 语句将不会被执行。我希望所有这些都是有道理的。 W

了解return 的工作原理!

【讨论】:

    【解决方案2】:

    你忘记了调用函数的括号:

    yearly_expenses = 12 * monthly_cost()
    

    那么你有无法到达的代码:最后一行print... 无法到达。

    def monthly_cost():
        monthly_expenses = float(loan + insurance + gas + maintanence)
        return float(monthly_expenses)
        print("You expend $"+format(monthly_cost, '.2f')+"in a month.")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      • 2023-03-05
      • 1970-01-01
      相关资源
      最近更新 更多