我目前是第一年学习python编程的学生,我们目前正在研究模块/功能。我的代码出现错误:
Traceback (most recent call last): File "C:/Users/User/Desktop/CISC 300 - Dolce/Turn In/A3 Darryl Lardizabal pg121 #4 rework.py", line 86, in <module>
main() File "C:/Users/User/Desktop/CISC 300 - Dolce/Turn In/A3 Darryl Lardizabal pg121 #4 rework.py", line 81, in main
yearly = totalYearly (totalMonthly) File "C:/Users/User/Desktop/CISC 300 - Dolce/Turn In/A3 Darryl Lardizabal pg121 #4 rework.py", line 50, in totalYearly
totalYearly = totalMonthly*ONE_YEAR TypeError: unsupported operand type(s) for *: 'function' and 'float'
我尝试过多种方式使用该行,包括totatYearly = totalMonthly * 12等等,但不确定它是否只是存在或代码可能有什么问题。
def totalYearly (totalMonthly):
totalYearly = totalMonthly*ONE_YEAR
return totalYearly
感谢您的帮助!
#-----------------------------------------------------------------------------------------
# Name: Darryl Lardizabal
# Date: 2-20-2017
# Reference: Chapter 3 page # 121 problem # 4
# Title: Auto Costs
# Constants: ONE_YEAR = 12.0
# Inputs: Monthly Costs for Loan Payment, Insurance, Gas, Oil, Tires, Maintenance.
# ProcessA: Calculate sum for both monthly and annual costs of loan payment,
# ProcessB: insurance, gas, oil, tires, and maintenance.
# Outputs: Monthly Costs and Annual Costs
#-----------------------------------------------------------------------------------------
#GLOBAL CONSTANTS
ONE_YEAR = 12.0
##The following modules gets the cost of monthly expenses and stores it in the getCost
#reference variable ----------------------------------------------------------------------
def getLP ():
loanPayment = float(input("Please enter your monthly loan payment: "))
return loanPayment
def getInsurance ():
insurance = float(input("Please enter how much you spend on your insurance per month: "))
return insurance
def getGas ():
gas = float(input("Please enter how much you spend on gas per month: "))
return gas
def getOil ():
oil = float(input("Please enter how much you buy in oil per month: "))
return oil
def getTires ():
tires = float(input("Please enter how much you spend on tires each month: "))
return tires
def getMaintenance ():
maintenance = float(input("Please enter how much you spend on maintenance per month: "))
return maintenance
##The totalMonthly module adds the cost of the monthly expenses and stores it in the
#totalMonthly reference variable ---------------------------------------------------------
def totalMonthly (loanPayment, insurance, gas, oil, tires, maintenance):
totalMonthly = loanPayment + insurance + gas + oil + tires + maintenance
return totalMonthly
##The totalYearly module adds the costs of the monthly expenses and multiples it by 12 and
#stores it in the totalYearly module reference variable ----------------------------------
def totalYearly (totalMonthly):
totalYearly = totalMonthly*ONE_YEAR
return totalYearly
##The showCosts module shows the total monthly and total yearly costs for the individual --
# -----------------------------------------------------------------------------------------
def showCosts (monthly, yearly):
print ("---------------------------------------------------------")
print ("Drum Roll Please...")
print (".........................................................")
print ("Your total monthly costs are: ", format(monthly,".2f"))
print ("---------------------------------------------------------")
print ("Your total yearly costs are: ", format(yearly,".2f"))
print ("---------------------------------------------------------")
return
##main module
def main():
print ("-------------Starting Main-------------------------------")
## Get Monthly Costs from User
loanPayment = getLP()
insurance = getInsurance()
gas = getGas()
oil = getOil()
tires = getTires ()
maintenance = getMaintenance()
##Calculate Monthly Costs of User
monthly = totalMonthly (loanPayment, insurance, gas, oil, tires, maintenance)
##Calculate Annual Costs of User
yearly = totalYearly (totalMonthly)
##Show Costs to User for both per month and per year
showCosts (totalMonthly, totalYearly)
main()
答案 0 :(得分:0)
当您计算年度值并显示结果时,您将传递函数而不是结果作为参数。修复它,它会起作用:
##Calculate Monthly Costs of User
monthly = totalMonthly (loanPayment, insurance, gas, oil, tires, maintenance)
##Calculate Annual Costs of User
#yearly = totalYearly (totalMonthly)
yearly = totalYearly (monthly)
##Show Costs to User for both per month and per year
#showCosts (totalMonthly, totalYearly)
showCosts (monthly, yearly)
更新以下是现在正在发生的事情的简短示例以及上述修复方法:
>>> def totalMonthly(x):
... return x + 1
...
>>> def totalYearly(monthly):
... return monthly * 12
...
>>> m = totalMonthly(3)
>>> totalYearly(totalMonthly)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in totalYearly
TypeError: unsupported operand type(s) for *: 'function' and 'int'
>>> totalYearly(m)
48