【发布时间】: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_cost是12 * monthly_cost()? -
你不能将函数乘以整数。也许你将函数调用的结果乘以整数。
标签: python function multiplication