HappyHands31 - 10 months ago 56
Python Question

# Interest Calculator In Python

Interest Calculator Let the user calculate the amount of money they will have in the bank after their interest has compounded for a certain number of years.

Note: A = P(1+r)^t where A = total amount, P = principal, r = rate, and t = time.

This is what I tried:

``````principal = input("How much money do you currently have in the bank?")
rate = input("What is your interest rate?")
time = input("Over how many years is the interest compounded?")
actual_principal = float(principal)
actual_rate = float(rate)
actual_time = int(time)

#TODO: Calculate the total amount and print the result
A = (actual_principal(1 + actual_rate) ^ actual_time)
print(A)
``````

I used actual_principal, actual_rate, and actual_time to convert the strings into (exact) integers...I used float instead of int because float will maintain an input with decimals, right?

But I got an error message on line 9

``````TypeError: 'float' object is not callable on line 9
``````

I'm assuming that the 'float' object refers to either actual_principal or actual_rate, or both, since those are the two values that I converted. Why, then, aren't they callable? How can I fix this program to calculate interest correctly? Thank you!

EDIT

Learned that power function in python is ** and that implied multiplication operators don't exist in python syntax. Reworked my equation to look like this:

``````final_actual_rate = actual_rate + 1
B = actual_principal * final_actual_rate
A = B ** actual_time
print(A)
``````

When entering 13.58 for "How much money do you currently have in the bank?", 0.3 for "What is your interest rate?", and 5 for "Over how many years is the interest compounded?", I got the answer 1714808.43561 - so the calculator works!

EDIT 2

Replaced my equation with this one - it's a better / cleaner way to create the interest calculator - although both will get you the same result:

``````A = math.pow(actual_principal*(1 + actual_rate) , actual_time)
print(A)
``````

``````import math
principal = input("How much money do you currently have in the bank?")
rate = input("What is your interest rate?")
time = input("Over how many years is the interest compounded?")
actual_principal = float(principal)
actual_rate = float(rate)
actual_time = int(time)

#TODO: Calculate the total amount and print the result
A = math.pow(actual_principal*(1 + actual_rate) , actual_time)
print(A)
``````

A few things wrong with what you have. `^` is not what you think it is in Python. `^` is an XOR binary operator not power. If you want power you use math.pow() module.

Also using `action_principal(1 + actual_rate)` is wrong. You are trying to call the function `action_principal` with the argument `1 + actual_rate` instead of multiplying it. Try `*` instead.