我试图通过用户的输入来计算年度和月度成本。我在getTotalMonthly
函数中收到错误。整个错误如下:
File "C:/Users/Zeke/Desktop/pls.py", line 44, in getTotalMonthly
totalMonthly=loanPayment + insurancePayment + gasPayment + oilPayment + tiresPayment + maintainancePayment
TypeError: unsupported operand type(s) for +: 'function' and 'function'
pls.py
文件包含:
#Get Loan Payment
def getLoanPayment():
loanPayment=0
loanPayment=float(input('Enter the amount of the loan payment: '))
return loanPayment
#Get the insurance payment
def getInsurancePayment():
insurancePayment=0
insurancePayment=float(input("Enter the amount of the insurance payment: "))
return insurancePayment
#Get the gas payment
def getGasPayment():
gasPayment=0
gasPayment=float(input("Enter the amount of the gas payment: "))
return gasPayment
#Get the oil payment
def getOilPayment():
oilPayment=0
oilPayment=float(input("Enter the amount of the oil payment: "))
return oilPayment
#Get the tires payment
def getTiresPayment():
tiresPayment=0
tiresPayment=float(input("Enter the amount of the tires payment: "))
return tiresPayment
#Get the maintainance payment
def getMaintainancePayment():
maintainancePayment=0
maintainancePayment=float(input("Enter the amount of the maitainance payment: "))
return maintainancePayment
#add up all the payments to get a monthly total
def getTotalMonthly(loanPayment,insurancePayment,gasPayment,oilPayment,tiresPayment,maintainancePayment):
totalMonthly=0
totalMonthly=loanPayment + insurancePayment + gasPayment + oilPayment + tiresPayment + maintainancePayment
return totalMonthly
#Calculate the annual costs
def getTotalAnnual(totalMonthly):
totalAnnual=0
totalAnnual=totalMonthly*12
return totalAnnual
#define the main function
def main():
loanPayment=0
insurancePayment=0
gasPayment=0
oilPayment=0
tiresPayment=0
maintainancePayment=0
loanPayment=getLoanPayment
insurancePayment=getInsurancePayment
gasPayment=getGasPayment
oilPayment=getOilPayment
tiresPayment=getTiresPayment
maintainancePayment=getMaintainancePayment
totalMonthly=getTotalMonthly(loanPayment,insurancePayment,gasPayment,oilPayment,tiresPayment,maintainancePayment)
totalAnnual=getTotalAnnual(totalMonthly)
print('the total monthly payment is',totalMonthly)
print('the total annual payment is',totalAnnual)
main()
答案 0 :(得分:3)
您需要使用括号调用所有函数。这不起作用:
insurancePayment=getInsurancePayment
你只是得到函数对象而不是它的结果。要修复它,您需要:
insurancePayment=getInsurancePayment()
您也不需要在Python中初始化变量,因此不需要这些:
loanPayment=0
insurancePayment=0
gasPayment=0
oilPayment=0
tiresPayment=0
maintainancePayment=0
您可以在需要时设置变量。