Код
Использование рекурсии, как вы просили:
def loan_calculator(y, payments, interest, monthly, period, counter=0, amount_left=[]):
# y -> starting loan (60,000)
# payments -> paying $700/month
# interest -> Interest rate on loan (yearly)
# monthly -> boolean, if monthly or yearly
# period -> how many years
# counter -> number of payments made
# amount_left -> after each payment how much is left to pay
if monthly:
period *= 12
interest /= (12*100)
# stopping condition:
if counter == period:
return y, amount_left
else:
y = (1+interest)*y - payments
amount_left.append(y)
counter += 1
return loan_calculator(y, 700, 1.2, True, 5, counter)
final_payment, all_payments = loan_calculator(6e4, 700, 1.2, True, 5)
print(all_payments)
print(final_payment)
Вывод
[59359.99999999999, 58719.359999999986, 58078.07935999998, 57436.15743935997, 56793.59359679933, 5615
0.38719039612, 55506.53757758651, 54862.04411516409, 54216.90615927925, 53571.12306543852, 52924.6941
88503956, 52277.618882692455, 51629.896501575146, 50981.52639807671, 50332.50792447478, 49682.8404323
9925, 49032.52327283165, 48381.555796104476, 47729.937351900575, 47077.66728925247, 46424.74495654172
, 45771.16970149826, 45116.94087119975, 44462.05781207095, 43806.51986988301, 43150.32638975289, 4249
3.476716142635, 41835.970192858775, 41177.80616305163, 40518.98396921468, 39859.50295318389, 39199.36
245613707, 38538.561818593196, 37877.100380411786, 37214.97748079219, 36552.192458272984, 35888.74465
0731256, 35224.633395381985, 34559.858028777366, 33894.41788680614, 33228.31230469295, 32561.54061699
7634, 31894.102157614627, 31225.996259772237, 30557.222256032004, 29887.779478288034, 29217.667257766
32, 28546.884925024082, 27875.431809949103, 27203.30724175905, 26530.510549000803, 25857.0410595498,
25182.898100609345, 24508.080998709953, 23832.58907970866, 23156.421668788367, 22479.578090457155, 21
802.057668547608, 21123.859726216153, 20444.983585942366] # This is a list of payments you owe each month
20444.983585942366 # This is your final payment