Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
loan
#21
Append is not inside a loop.
Reply
#22
I am not understanding, please rephrase.
Should append be inside the for loop or outside?
At the moment is inside as far as I can see.
Reply
#23
def this_is_a_function():
    container = []
    for item in range(5):
        container.append(item) # this is inside a loop
        
    return container

print(this_is_a_function())
Output:
[0, 1, 2, 3, 4]
Reply
#24
thanks for the explanation, but your code applied to my case still returns a single value.
I have tried to work with the indentations more but that's the outcome I am getting.

def f(balance,r,A,n):
        r=5.0
        A=1000.0
        n=6
        balance=10000
        L=[]
        
        for year in range(n):
            balance=(balance)*(r/100.0 +1)- A
            L.append(year)
        return L
print(L)
Reply
#25
Post code that can be run.
I removed the input parameters because in your code they are useless and you don't listen to why, so i just got rid of them.
Added an actual call to the function.

def f():
        r=5.0
        A=1000.0
        n=6
        balance=10000
        L=[]
         
        for year in range(n):
            balance=(balance)*(r/100.0 +1)- A
            L.append(year)
        return L

print(f())
Output:
[0, 1, 2, 3, 4, 5]
Reply
#26
to Yoriz:

our codes are identical.
The only difference is the absence of parameters in yours which does not influence at all
the output.

Maybe you misunderstood what I am looking to print.
I do not need to print the number of iterations but
all the values that the formula calculates after 1,2,3...n iterations.
My code as it is now, it only prints the n th value.
Reply
#27
(Apr-04-2019, 07:08 PM)mcgrim Wrote: I do not need to print the number of iterations but
all the values that the formula calculates after 1,2,3...n iterations.

Then append balance instead of year.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#28
This code is supposed to Iterate a (decreasing) list of numbers and stop right before they go below zero.
I am also trying to print the number of iterations needed to get there.
This code not only it prints out one negative number, but the number of iterations printed are always 22, even though the code iterates less times (see value of n) and print(L) always prints the value correspondent
to n=6, no matter which value I choose to give n.

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
Reply
#29
Where's the initialization part? If I tried to run that, I'm sure I'd get a balance is not defined error on line 16. Or are the first 4 lines of the function supposed to be above the function?
Reply
#30
no, the code runs smoothly the way you see it.
Everything is defined, and line 16 actually works the way I want as it prints all the values
for each iteration.
Reply


Forum Jump:

User Panel Messages

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