2017-12-01 9 views
-1

したがって、ユーザーが毎月どれくらいの金額を費やしているかを計算し、その数値を使って年間の費用を計算する必要があります。私は彼らの年間経費を取得するために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() 
+0

は '12 * monthly_cost'を' 12 * monthly_cost() 'にする必要がありますか? – castis

+0

関数を整数で乗算しません。関数呼び出しの結果に整数を掛けることもできます。 – miradulo

答えて

0

到着保つあなたが忘れてしまったことを行うために、

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.") 
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は、この機能を終了する場合にのみ必要です。 printステートメントは、returnprintステートメントに達する前に実行されません。私はこれがすべて意味があることを願っています。 W

returnの仕組みを理解してください!

関連する問題