Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loan
#31
Ok. type is like cat on Windows, it just shows the contents of the file.

E:\Projects\etc>type spam.py
def f(balance,r,A,n):
        r=5.0
        A=1000.0
        n=18
        balance=10000
        L=[]

        for year in range(n):
            balance=(balance)*(r/100.0 +1)- A
            L.append(balance)
            if balance <=0:
                balance=0
                break
        return L

print(f(balance,r,A,n))
print(L,n) #should print the last non negative value and the iterations
            #needed to get there
E:\Projects\etc>python spam.py
Traceback (most recent call last):
  File "spam.py", line 16, in <module>
    print(f(balance,r,A,n))
NameError: name 'balance' is not defined
Reply
#32
I concur with nilamo, that code is broken. It's been broken since you first posted it in the other thread. Defining in the function does not define outside the function. Defining in the function removes any point in having parameters.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#33
I have now commented the values inside the function and left the parameters the way they were.
I am trying to use the same code to calculate A, in other words, how big does A need to be in order
for the n the iteration to be zero or less?

The loop on the last line does not work, it always says that 'return' is out of the function,
even though the indentation looks correct to me.

def f(balance,r,A,n):
        #r=5.0
        #A=1000.0
        #n=12
        #balance=10000
        L=[]
          
        for year in range(n):
            balance=(balance)*(r/100.0 +1)- A
            L.append(balance)
            if balance <=0:
                balance=0
                break
        return L
       

a=len(f(balance,r,A,n))
print(a)
b=(f(1000,5,A,20)[a-1])


if b <=0:
    return A
    print(A)
Reply
#34
The return on line 23 is not in a function. It is under the if statement, but the if statement is not indented to be under the function. And you already have a return statement in a function.

You don't return anything from a whole Python program (module). I would just delete line 23.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#35
return A is not part of a function that is what the error is telling you.
Reply
#36
ok, got it. But now how do I calculate A then?
Do I have to rewrite the code entirely ?
I wrote line 23 for the purpose of doing so, but now
you are telling me that I must erase it, so how should I proceed ?
Reply
#37
I am still trying but no luck.
This is my new code and the output I obtain is empty.

def f(balance,r,A,n):
        #r=5.0
        #A=1000.0
        #n=12
        #balance=10000
        L=[]
          
        for year in range(n):
            balance=(balance)*(r/100.0 +1)- A
            L.append(balance)
            if balance==10000 and n==5 and r==5:
                print(A)
            if balance <=0:
                balance=0
                break
            return L
Reply
#38
The code shown does nothing, you have not called the function with any values or output the result of the function.
Reply
#39
I know it does nothing, that is exactly what I wrote. Now I am trying to fix this problem.
I thought I indicated the values I want to use on line 11.
All I am trying to do is to calculate A, how large does it need to be in order for the last element
of the list to be either 0 or a negative number?
Let me please know how to start, if what I wrote is totally off the track, or you can tell me what exactly should be corrected.
Reply
#40
If you just create a function and don't call it nothing will happen
def a_func(a_number):
    return a_number + 10
Output:
The function need to be called with arguments if the function requires them
def a_func(a_number):
    return a_number + 10

a_func(2)
Output:
again there was no output so a print is required to see the return from the function
def a_func(a_number):
    return a_number + 10

print(a_func(2))
Output:
12
Reply


Forum Jump:

User Panel Messages

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