Jerry Silvera
Jerry Silvera

Reputation: 53

Python For Loops with interest payments

I 'm trying to run this program by having it accept 3 parameters for my functions and using it to calculate compounded interest over the time period. After which calculating and displaying the total at the end of the time period.

This is my code:

principal=float(input("Enter an initial principal value: "))
interest=float(input("Enter an interest rate: "))
years=int(input("enter how many years it will take: "))

def payment(principal, interest, years):
    n=principle*((1+interest)**years)

    for n in range(principal,total):
        print(n)
        total=n+total

payment(principal, interest, years)

The issues I have with this model is that it gives me an error of :

    for n in range(principal,total):
TypeError: 'float' object cannot be interpreted as an integer

and furthermore I'm not too sure if it will compound the interest for each period and add that to the principal when it calculates next years compounded interest.

Upvotes: 1

Views: 14229

Answers (4)

Creative KDR
Creative KDR

Reputation: 1

P = int(input("Enter the Principle Amount: "))
r = int(input("Enter the Rate of Interest: "))
n = int(input("Enter the year: "))


def Amount(p, r, n):
    t = int(input("Enter the no. of time: "))
    R = r / 100
    for i in range(t):
        a = float(1 + R/n)
        b = n * t
        c = a**b
        CI = float(p * c)
    print("The investment balance after",t,"years is $",CI)


Amount(P, r, n)

Upvotes: 0

kennes
kennes

Reputation: 2145

Try this instead:

def payment(principal, interest, years):

    for period in range(years):
        total = float(principal) * float((1+interest)**float(period+1))
        print 'Period:', period+1
        print 'Total:', total

    return  total

Upvotes: 5

Prathik Rajendran M
Prathik Rajendran M

Reputation: 1162

range in python accepts integer values for start, stop and step. As per the docs

This is a versatile function to create lists containing arithmetic progressions. It is most often used in for loops. The arguments must be plain integers.

https://docs.python.org/2/library/functions.html#range

When you say

for n in range(principal,total):
        print(n)
        total=n+total

You are passing float values to the range function. Also if you are planning to print yearly total then the way you are using the for loop is incorrect. All this will do is if principle and total are integers like (100,200) it will put values like 101,102 etc.. in n. And the code is doing something apart from giving you the yearly breakdown.

The following code should help you do what you want

''
Let i be in percentage
'''
def computeCompound(s,i):
    v = s
    while True:
        v = v*(1.0 + float(i)/100)
        yield v

c = computeCompound(100,20)

for period in range(1,4):
    print period
    print c.next()

Upvotes: 0

colintobing
colintobing

Reputation: 167

Range must use integer. See this link http://www.pythoncentral.io/pythons-range-function-explained/

Upvotes: 1

Related Questions