私はEdXコースの再帰について勉強し始めました.12ヶ月間最低支払額を支払った後の残りの残高を計算する反復関数を書いてきました。反復を再帰Pythonに置き換える
私は簡単に反復でそれを行うことができましたが、再帰的な方法で私の頭を包むようには見えません。
正しい方向に指摘してください。ここで
は私の反復機能
def remaining_balance_iter(balance,annualInterestRate, monthlyPaymentRate):
'''
This code will take any balance and annual interest rate and calculate the
balance after one year of making the minimum payments
'''
month = 1
monthly_interest_rate = annualInterestRate/12.0
while month <= 12:
minimum_monthly_payment = monthlyPaymentRate * balance
monthly_unpaid_balance = balance - minimum_monthly_payment
balance = monthly_unpaid_balance + monthly_interest_rate*monthly_unpaid_balance
print("Month {} Remaining balance: ".format(month) + str(round(balance,2)))
month += 1
print ("Remaining balance " + str(round(balance,2)))
である私は、再帰関数の試みを行ってきましたが、それは仕事を必要とし、私は笑
def remaining_balance_recur(balance,annualInterestRate, monthlyPaymentRate, month):
'''
This code will take any balance and annual interest rate and calculate the
balance after one year of making the minimum payments
'''
month = 1
monthly_interest_rate = annualInterestRate/12.0
while month <= 12:
minimum_monthly_payment = monthlyPaymentRate * balance
monthly_unpaid_balance = balance - minimum_monthly_payment
interest = monthly_interest_rate*monthly_unpaid_balance
balance = remaining_balance_recur(monthly_unpaid_balance, annualInterestRate, monthlyPaymentRate, month + 1) + interest
print ("Remaining balance " + str(round(balance,2)))
インデントを修正しましたか?あなたのコードはコンパイルされません。 –