loan - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: loan (/thread-17222.html) |
RE: loan - mcgrim - Apr-03-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? RE: loan - perfringo - Apr-03-2019 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 RE: loan - mcgrim - Apr-03-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)) RE: loan - Yoriz - Apr-03-2019 (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 (Apr-03-2019, 06:56 AM)mcgrim Wrote: to Yoriz: 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)
(Apr-03-2019, 04:12 PM)mcgrim Wrote: Thanks, that was very helpful. 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. RE: loan - ichabod801 - Apr-03-2019 (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. RE: loan - mcgrim - Apr-03-2019 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? RE: loan - ichabod801 - Apr-03-2019 (Apr-03-2019, 06:22 PM)mcgrim Wrote: so why do you think is a mistake to place variables right after the function? 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? 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. RE: loan - mcgrim - Apr-03-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) RE: loan - ichabod801 - Apr-03-2019 (Apr-03-2019, 07:13 PM)ichabod801 Wrote: make a list in the function RE: loan - mcgrim - Apr-03-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) |