Reputation: 11
A school project asks me to do a function that should return the absolute value of the monthly payment rounded to two decimal points.
Can anyone explain me why my code doesn't properly work? Thanks in advance!
interest_rate= int(input('Type your interest rate (in %) '))
principal = int(input('Type your principal '))
period = int(input('Type for how many months '))
def calculate_payment(principal, interest_rate, period):
final_amount = (1 + interest_rate) ** period
return principal * (interest_rate * final_amount) /
(final_amount- 1)
final_amount = str(round(final_amount, 2))
print("your amount that has to be paid after each month is: ", final_amount)
Upvotes: 0
Views: 37
Reputation: 18377
I believe you are both missunderstanding the formula for compound interest (aside from stackoverflow) nor is the period for the interest rate detailed (is the interest rate monthly? annually?). If the interest rate is monthly and you will return the interest that will be paid after a month, then the variable period is of no use. Regarding the code, you are not defining the function correctly, nor calling it correctly to make it work. Furthermore if it's compound interest, the value to be paid each month (of interests) will never be equal since unless the interests are being withdraw and not reinvested. Iff that's what you want then check CASE 2. Case 1 assumes you are returning the full amount when the operation finishes.
This is what I think you are looking for:
interest_rate= int(input('Type your interest rate (in %) '))
principal = int(input('Type your principal '))
period = int(input('Type for how many months '))
def calculate_payment(principal, interest_rate, period):
final_amount = principal * ((1+interest_rate/100) ** period)
return round(final_amount,2)
print("your amount that has to be paid at the end of the period is: ", calculate_payment(principal, interest_rate,period))
Output:
Type your interest rate (in %) 10
Type your principal 1000
Type for how many months 2
your amount that has to be paid at the end of the period is: 1210.0
interest_rate= int(input('Type your monthly interest rate (in %) '))
principal = int(input('Type your principal '))
period = int(input('Type for how many months '))
def calculate_payment(principal, interest_rate, period):
final_amount = principal * (((1+interest_rate/100)) - 1)
return round(final_amount,2)
print("your amount that has to be paid after each month is: ", calculate_payment(principal, interest_rate,period))
Output:
Type your monthly interest rate (in %) 10
Type your principal 1000
Type for how many months 2
your amount that has to be paid after each month is: 100.0
Upvotes: 1