Calculation loop in python new values

Related searches

I would like to do the calculation loop using my function.

b=list(data.iloc[1])

def balance(rate, payment, os):
    interest_amount=os*rate/100/12
    principal_amount=payment-interest_amount    
    next_balance=os+interest_amount-principal_amount
    return next_balance

c=balance(b[9], b[11], b[8])
d=balance(b[9], b[11], c)
e=balance(b[9], b[11], d)

I would have to start with b[8] as the amount for calculation. After I got the next amount from the function balance, the next amount will be the beginning of the third calculation and so on until the next amount eqaul of less than 0. It should stop the loop.

I need to append calculated values since b[8] until the last (before getting 0 or less).

Any suggestion on this, thank you!

Edit: based on Zaraki Kenpachi

b[8] is amount of money given 17183 b[9] is rate of interest given 3.39 b[11] is payment per month given 5759

The output, which I am trying to do is:

[17183, 11,521, 5,826, 99]

Perhaps something like this

x = b[8]
output = []
output.append(x)
while x > 0:
    x = balance(b[9], b[11], X)
    output.append(x)

1.13. Loops and Sequences — Hands-on Python Tutorial for Python 3, There are two Python statement types to do that: the simpler for loops, which we take up Each time the heading line executes, it implicitly assigns a new value to the Instead, as with a function definition or any other format with an indented � break ends the loop entirely. When Python executes break, the for loop is over. continue ends a specific iteration of the loop and moves to the next item in the list. When Python executes continue it moves immediately to the next loop iteration, but it does not end the loop entirely.


Here you go:

def balance(rate, payment, os):
    interest_amount=os*rate/100/12
    principal_amount=payment-interest_amount
    next_balance=os+interest_amount-principal_amount
    return next_balance


next_balance = 17183 # = b[8]
results = []
results.append(next_balance)
while next_balance > 0:
    next_balance = balance(3.39, 5759, next_balance) # b[9], b[11]
    if next_balance > 0:
        results.append(next_balance)

Output:

[17183, 11521.08395, 5827.1780743175, 101.10163043739522]

The Basics of Python For Loops: A Tutorial, Learn how to master Python for loops and statements like break and You create a for loop by first defining the iterable object you'd like to loop this value to total_range within our for loop, and then calculate the value using� Python For Loops. A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).. This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.


Python doesn't have a builtin way to unfold / iterate but the normal way you'd implenent a specific unfold is through a generator, which can keep computational state and yield values:

def balances(balance, rate, payment):
    while True:
        interest_amount = balance*rate/100/12
        principal_amount = payment-interest_amount    
        balance = os+interest_amount-principal_amount
        # TO DO: end the computation when balance <= 0, maybe find a way to 
        # note extra in the last payment, or reject the last payment entirely
        # and handle that case separately outside the generator
        yield balance

then you can either call next() on your balances iterator to get successive values

bs = balances(b[8], b[9], b[11])
c = next(bs)
d = next(bs)
e = next(bs)

or iterate the entire thing

for balance in balances(b[8], b[9], b[11]):
    if balance < 0: 
        "do something when the last payment was excessive and stop the loop"
    ...

Python "for" Loops (Definite Iteration) – Real Python, Each time through the loop, the variable i takes on the value of the next object in In fact, it is possible to create an iterator in Python that returns an endless� Let’s see how to create a column in pandas dataframe using for loop. Such operation is needed sometimes when we need to process the data of dataframe created earlier for that purpose, we need this type of computation so we can process the existing data and make a separate column to store the data.


7. Iteration — How to Think Like a Computer Scientist: Learning with , A new assignment makes an existing variable refer to a new value (and stop referring In Python, an assignment statement can make two variables equal, but of the sum so far, either on a piece of paper, in your head, or in your calculator. #!/usr/bin/python for num in range(10,20): #to iterate between 10 to 20 for i in range(2,num): #to iterate on the factors of the number if num%i == 0: #to determine the first factor j=num/i #to calculate the second factor print '%d equals %d * %d' % (num,i,j) break #to move to the next number, the #first FOR else: # else part of the loop print


Automate Data Tasks With Loops in Python, You can add do this with your loop with only two new lines of code: First, you create an empty list that will receive new values using listname = [] . Then, you can add a new line of code to append each value after it is calculated using listname. append(value) . Loops are important in Python or in any other programming language as they help you to execute a block of code repeatedly. You will often come face to face with situations where you would need to use a piece of code over and over but you don't want to write the same line of code multiple times.


Definite iteration loops are frequently referred to as for loops because for is the keyword that is used to introduce them in nearly all programming languages, including Python. Historically, programming languages have offered a few assorted flavors of for loop.