2017-02-17 6 views
0

私は現在、Pythonプログラミングを学んでいる初年度の学生で、現在モジュール/関数を扱っています。私は読み、私のコードでエラーを取得しています:私はtotatYearly = totalMonthly * 12などなどなど、複数の方法で行と遊ぶことを試みた現在、次のようなTypeErrorを取得しています:*: 'function'と 'float'のサポートされていないオペランドタイプ

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' 

が、それはそこだけかどうかわかりませんか、何が他のコードで間違っている可能性があります。

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

年の値を計算しているときに、結果をパラメータとして渡すのではなく、結果を表示しているとき。それを修正し、それが動作します:

##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 
+0

申し訳ありませんが、私はまだこれにかなり新しいです。私は関数を渡しているが、代わりに結果を入力する方法がわからないことを理解しています。 これまでのところ、ありがとうございました。 –

+0

@DarrylLardizabal上記の変更は関数の代わりに結果を渡します。上記のコードで関連する場所を置き換えると、期待通りに機能します。私は何が起こっているのか、そしてその変化がどのように修正されているのかを説明する簡単な例を追加しました。 – niemmi

+0

ありがとう、これは助けた。 –

関連する問題