Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loan
#1
I am writing a program that calculates the remaining loan after a certain number of years n.
The initial borrowed amount is called 'balance' in this code and the formula after n years is
balance(year n)=balance (year n-1)*(r/100 +1) + A where A is a constant.
If for example this year I borrowed 1000000, the remaining balance next year would be 10000(r/100 +1) + A
and the year after would be whatever I get from this calculation plugged into the formula.


This is the code that I obtained and I am always getting 0.
I am also not sure how to arrange the code to find out how much loan I have left after n years (this n can be any integer)

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)
    balance=round(balance,2)
    if balance<0:
        balance=0
    
print(balance)
Reply
#2
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
Reply
#3
That code isn't giving 0, that code just fails. It fails because none of the variables (balance, r, n, or A) are defined before you try to use them on lines 7 and 8.

If you have similar code where f is always returning 0, I expect it is because of r/100. If you are running the code in Python 2.7, that will always be 0, because dividing with integers always gives a truncated integer in 2.7.

You should run the code in Python 3.0+, which will give a float for r/100. If you can't do that, change the 100 to 100.0, so you are dividing by a float.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Also, with 31 posts, I'm surprised you don't know this by now - you should be providing runnable snippets of code, and any associated errors / output of that code. In this case, you posted code that produces a NameError but something else is happening.
Reply
#5
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 ichabod801:
I am using the 3.7 version.
I wrote 100.0 like suggested and I obtain the same outcome.
you said
Quote: It fails because none of the variables (balance, r, n, or A) are defined before you try to use them on lines 7 and 8.
what do you mean? Those variables are declared in line 2,3,4.

to micseydel:

my snipped of code runs and all I obtain is what I already wrote.
In case I would have ran into an error (or anything else for that matter)
I would have said so.


to everyone:
my code is still producing the same output and I do not know how to fix it.
Reply
#6
(Apr-03-2019, 06:56 AM)mcgrim Wrote: what do you mean? Those variables are declared in line 2,3,4.

If you got fundamentals wrong then your code mostly delivers unexpected results.

If you don't believe ichabod801 then refer to documentation: Python Scopes and Namespaces, Resolution of Names

Quote:The local namespace for a function is created when the function is called, and deleted when the function returns or raises an exception that is not handled within the function. (Actually, forgetting would be a better way to describe what actually happens.)

What happens in Vegas stays in Vegas.
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
#7
I did not give any indication on where my beliefs stand. I am only asking explanations to what is written.
Then according to what you said, if I declare such variables globally (before the function itself)
I should be solving the problem, but instead nothing changes.
Here is my new code. I just placed the variables above.

r=13.0
A=1000.0
balance=9999999999.0




def f(balance,r,n,A):
    return (balance)*(r/100.0 +1) - A

while balance>0:
    balance-=f(balance,r,n,A)
    balance=round(balance,2)
    if balance<0:
        balance=0
    
print(balance)
Reply
#8
(Apr-03-2019, 07:30 AM)mcgrim Wrote: Those variables are declared in line 2,3,4.

I did not give any indication on where my beliefs stand. I am only asking explanations to what is written.

There were no (global) variables (balance, r, n, or A) in your initial code. They were assigned within function and therefore existed only within function. You can't access these names from outside of this function (unless using some techniques which are frowned upon). By stating that you declared them indicates that you believe it to be true as reality is somewhat different.

Usually functions are written using parameters and called using arguments. Generally it's done this way:

>>> def f(balance,r,A):                  # defining function with parameters
...    return (balance)*(r/100.0 +1) - A
...
>>> f(10000, 13.0, 1000.0)               # calling function with arguments
10299.999999999998
Some observations:

- you defined n as parameter but never used it. While calling this function you should provide n otherwise function call will raise TypeError about missing required positional argument (in version above this parameter is skipped)

- for clarity it's good use keyword arguments. What is r? What is A?

- I have no idea what function return represents

You can read documentation about arguments and parameters
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
#9
for what the variables stand for, kindly refer to my first post of this thread.
n is used for the number of years and the question to be answered is: how much money do I still
own after n years have passed? (again, refer to my first post).
I have not used n yet because I don't know how to.
Momentarily I have erased it to try out your suggestion and this is the code I have.
However, it still does not work.

def f(balance,r,A):
    r=13.0
    A=1000.0
    balance=9999999999.0
    return (balance)*(r/100.0 +1) - A
    while balance>0:
        balance-=f(balance,r,n,A)
    balance=round(balance,2)
    if balance<0:
       balance=0
       
print(balance)

in order to make matters as clear as possible, I believe that is safe to say that the following code works
def f(balance,r,A):
    r=13.0
    A=1000.0
    balance=9999999999.0
    return (balance)*(r/100.0 +1) - A
in fact if I want to print out the value
print(f(balance,r,A))
I obtain 11299998998.869999.

But this part of the code

while balance>0:
        balance-=f(balance,r,n,A)
        balance=round(balance,2)
    if balance<0:
       balance=0
print(balance)
keeps giving me the same issue.
Reply
#10
(Apr-03-2019, 08:07 AM)mcgrim Wrote: for what the variables stand for, kindly refer to my first post of this thread.

If you return to this function in say about three months later (or somebody else tries to understand what this function does) then you probably will have no clue what is meaning of these letters. In order to be reusable code must be self-explanatory. One method to achieve this result is to use descriptive names.

What is easier to understand what function does from it's definition f(b, r, n) or name_of_my_secret_love(my_name, marital_status, number_of_times_married) Smile ?
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


Forum Jump:

User Panel Messages

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