Using functions in 'while' loop python -
so want create code calculate minimum monthly payment , remaining balance, given annual interest rate, principal amount , monthly payment rate. desired output is:
month: 1 minimum monthly payment: 168.52 remaining balance: 4111.89 month: 2 minimum monthly payment: 164.48 remaining balance: 4013.2
and on until month 12.
i know there's way without defining functions whole function thing messing me wanted try it. current code -
a=0 while a<=11: def min_mth_pay(balance,monthlypaymentrate): x = balance * monthlypaymentrate return x def balance(balance,min_mth_pay,annualinterestrate): y=(balance - min_mth_pay)*((annualinterestrate/12)+1) return y +=1 print('month:' + str(a) + 'minimum monthly payment:' + str(x) + 'remaining balance:' + str('y'))
i'm not sure if can use functions in such format? error pops out saying name 'x' undefined. new @ python here appreciate clarifications! :)
you're confusing defining functions calling them. should define functions separately, call them within loop.
def min_mth_pay(balance,monthlypaymentrate): x = balance * monthlypaymentrate return x def balance(balance,min_mth_pay,annualinterestrate): y=(balance - min_mth_pay)*((annualinterestrate/12)+1) return y a=0 while a<=11: +=1 x = min_mth_pay(balance,monthlypaymentrate) y = balance(balance,min_mth_pay,annualinterestrate) print('month:' + str(a) + 'minimum monthly payment:' + str(x) + 'remaining balance:' + str(y))
note it's not clear balance, monthlypaymentrate, min_mth_pay, , annualinterestrate coming in code.
Comments
Post a Comment