Posts: 203
Threads: 41
Joined: Mar 2019
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?
Posts: 1,950
Threads: 8
Joined: Jun 2018
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.
Posts: 203
Threads: 41
Joined: Mar 2019
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))
Posts: 2,168
Threads: 35
Joined: Sep 2016
(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.
Posts: 4,220
Threads: 97
Joined: Sep 2016
(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.
Posts: 203
Threads: 41
Joined: Mar 2019
Apr-03-2019, 06:22 PM
(This post was last modified: Apr-03-2019, 06:46 PM by mcgrim.)
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?
Posts: 4,220
Threads: 97
Joined: Sep 2016
(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.
Posts: 203
Threads: 41
Joined: Mar 2019
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)
Posts: 4,220
Threads: 97
Joined: Sep 2016
(Apr-03-2019, 07:13 PM)ichabod801 Wrote: make a list in the function
Posts: 203
Threads: 41
Joined: Mar 2019
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)
|