Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loan
#11
r is the int rate (keep in mind that the formula divides this number by 100, so if let's say you want to use
13%, just write 13)
n is the number of years (not using that for now)
A is just a constant paid every year, it can be called anything.

just to state the problem again, let's say I borrow an original balance of 10000 in the year 2010.
in the year 2011 my loan will go down to 10000*(13/100 +1) - A.

and for the year 2012 I have to plug in this newly obtained number into the formula and so on...

So let' say that I need to find out my current loan in the year 2015, how should I correct my code?
Reply
#12
It seems to me that it easier to write working example then to correct mistakes in your code.

Following is the simple function which calculates reminder of the loan based on loan, annual interest rate, constant payment and number of years:

def loan_reminder(loan, annual_interest_rate, payment, years):
    principal = loan
    for year in range(years):                           # repeat process for every year
        interest = principal * annual_interest_rate     # calculate interest accrued 
        principal = principal + interest - payment      # calculate new principal
        if principal <= 0:                              # check whether loan is repaid
            return 0                                    # if loan repaid return 0 as loan reminder
    return principal                                    # return loan reminder after years

loan_reminder(10000, 0.05, 1000, 5)
7237.184375000001
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#13
Thanks, that was very helpful.
I have rewritten my code and the structure is almost identical to yours,
however, my for loop seems not to be working.
No matter what value I put in, it only counts the first iteration, and I obtain
9500 no matter the range.
Where is my mistake?

def f(balance,r,A,n):
        r=5.0
        A=1000.0
        n=6
        balance=10000
     
        for year in range(n):
            balance=(balance)*(r/100.0 +1)- A
            if balance <=0:
                return 0
            return balance
        
print(f(balance,r,A,n))
Reply
#14
(Apr-02-2019, 10:05 PM)Yoriz Wrote: Whatever values (balance,r & A) are passed into f are immediately assigned to constant values inside of f
The function f as it stands returns -129000.0 which will be set to 0

(Apr-03-2019, 06:56 AM)mcgrim Wrote: to Yoriz:
I am not sure what you mean by -129000,
if you compute 1000000*1.13 -1000 (in the description I wrote +A, my mistake)
you get 1129000. How did you obtain a negative number?

to everyone:
my code is still producing the same output and I do not know how to fix it.

This is what i mean by -129000, notice how i can can assign random stuff to the variables and still get the same answer because as stated in my original post
"Whatever values (balance,r & A) are passed into f are immediately assigned to constant values inside of f"
balance = 1000000
r = [1, 2, 3, 4, 5, 6]
A = 'toe nails'
n = 6 * 'banana'

def f(balance,r,n,A):
    r=13
    A=1000
    balance=1000000
    return (balance)*(r/100 +1) - A
 
while balance>0:
    balance-=f(balance,r,n,A)
    print(balance)
    balance=round(balance,2)
    if balance<0:
        balance=0
     
print(balance)
Output:
-129000.0 0
(Apr-03-2019, 04:12 PM)mcgrim Wrote: Thanks, that was very helpful.
I have rewritten my code and the structure is almost identical to yours,
however, my for loop seems not to be working.
No matter what value I put in, it only counts the first iteration, and I obtain
9500 no matter the range.
Where is my mistake?

def f(balance,r,A,n):
        r=5.0
        A=1000.0
        n=6
        balance=10000
     
        for year in range(n):
            balance=(balance)*(r/100.0 +1)- A
            if balance <=0:
                return 0
            return balance
        
print(f(balance,r,A,n))

The mistake is assigning these values inside function f
r=5.0
A=1000.0
n=6
balance=10000

which just over writes any values passed in, each time the function is called it will just set the variables to these values.
Reply
#15
(Apr-03-2019, 04:12 PM)mcgrim Wrote: No matter what value I put in, it only counts the first iteration

Your return statement is part of the loop, so it is executed as part of the first iteration. You need to unindent it one level.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#16
To ichabod801:
after following your advice of removing one indentation, the code is running the way it should,
so why do you think is a mistake to place variables right after the function?
Where would you place them otherwise, since they have to be included in the parameters?

also, how come that only one value is returned/printed out?
if the range n is 5 shouldn't I see 5 values?
What should I then do to see all values within a range?
Reply
#17
(Apr-03-2019, 06:22 PM)mcgrim Wrote: so why do you think is a mistake to place variables right after the function?
Where would you place them otherwise, since they have to be included in the parameters?

Because, as Yoriz explained, if you put them at the start of the function like that, they can never be changed. You can only run the function with those parameters. You would normally put the values in the call to the function. Then they would be assigned to the parameters and used in the function. That way you can run the function for different values.

def add_two(x):
    return x + 2

print(add_two(3))    # prints 5
print(add_two(801))  # prints 803
(Apr-03-2019, 06:22 PM)mcgrim Wrote: also, how come that only one value is returned/printed out?
if the range n is 5 shouldn't I see 5 values?
What should I then do to see all values within a range?

The return statement stops the execution of the function. That's why you only see one value. If you want to see all of the values, make a list in the function, append the values to the list, and return the list of the values.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#18
I have created a list and used append, however I still get one value.

def f(balance,r,A,n):
        r=5.0
        A=1000.0
        n=6
        balance=10000
     
        for year in range(n):
            balance=(balance)*(r/100.0 +1)- A
        return balance
        
L=[]
y=f(balance,r,A,n)
L.append(y)
print(L)
Reply
#19
(Apr-03-2019, 07:13 PM)ichabod801 Wrote: make a list in the function
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#20
done, but now I am getting an empty outcome.
def f(balance,r,A,n):
        r=5.0
        A=1000.0
        n=6
        balance=10000
     
        for year in range(n):
            balance=(balance)*(r/100.0 +1)- A
        
        L=[]
        L.append(balance)
        return L
        print(L)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020