Iterate - Loop -Python

Hi im a totally Python beginner , and my english is really bad so i hope you can actually understand my question.

So im trying to make a loop “n” number of times. So im using a slider which goes in the code block 0…n; so this is my IN[0] for my python formula. I have also a IN [1] which is a constant in the hole process.
I actually want that every z= IN [1] + the result of “y” in every cicle
so for the first cycle there is actually no y so it would be z= IN [1]
for the second cycle it would be z=IN [1]+y0
for the third one it would be z= IN[1] +y0+y1
for the fourth cycle" it would be z= IN[1] +y0+y1+y2
and so on until “yn”

But i actually don´t how is the correct way to write these in python…i would say the rest of my formula (look the screenshot) works…the problem is just that part in blue!

Any Help? Thanks

Simply use n in your marked area…instead of y(0…n)

do you mean? z=IN[1] + yn i allready tried these but i didnt work also…
or do you mean? z=IN[1] + n because these is not what i want…i want the results to use the results of every “y” to get “z”

@Jonathan.Olesen ?

Then I do not follow what you want… As the first iteration will have no value for y…

@Jonathan.Olesen mmm lets see how do i explain the better …
“n” is just the number of times the cycle would be repeated
and i want to find out “y” and “z” for every cycle
so the first thing is to find “z” for the first cycle… so “z0”=IN[1] + “y0” y0 = 0 (that is what i mean with there is no “y”)
then when i have the “z” of the first cycle i can calculate “y1”= (“z0”/3)*IN[1]
now the results of “y” and “z” should be use for the next cycle , thats why i save them in Y.append and Z.append
so for the secund cylce “z1”=IN[1] + “y0”+“y1” y1= the result of “y” which i got at the first cycle
“y2”= (“z1”/3)*IN[1]
for the third cycle “z2”=IN[1] + “y0”+“y1” + “y2”
and so on …“n” times

i hope now you can understand me…

@Jonathan.Olesen that is exactly the thing for the first iteration i have no “y” so the first “z” would be z= IN[1] , by the second iteration i should start to add the first gotten “y” and so on

That is saved in the x and y.

Simply, before your loop add a initial y value… y=0

Next it would read z = z + IN[1] + n

give me a second to illustrate :slight_smile:

Also keep in mind that (0…1) is actually two loops, not one.

Is this what you’d like?

EDIT… Sorry… You have to watch those integers :wink:

@Jonathan.Olesen thanks for your effort…but i was lot easyer as i thought…maybe me english was not clear enought to explain you what i was trying…

here the solution

All roads lead to Rome :slight_smile: glad you solved it

Hello Pablo!

Awesome solution :slight_smile:, here’s a little more feedback if you wish to have it (I’ve annotated up a version how I would approach this problem).

A couple of points to note:

  1. You don’t need any of the imports at the top if you are simply using Math
  2. For clarity reasons I would give my IN[0] and IN[1] variable names to call later
  3. You can use the ‘range()’ function inside of Python if you wish to contain it all within the one node
# Creating a range in Python (Same as: 0..n in DesignScript) is done
# using the 'range()' function. The difference here is that you have to
# use a '+ 1' at the end because in Python a range is 'Open Ended', which
# means the last value is not included.
loopRange = range(IN[0] + 1)
# We also define a new variable for our constant
constant = IN[1]

# To create 'catchment lists', we can call them on the same line as follows
listA, listB = [], []

# Then we run a 'for' loop for every item (number) in our range (loopRange)
for number in loopRange:
    # We calculate our 'ValA' by adding the constant to our summed values
    # in 'ListA'
    valA = constant + sum(listA)
    # We calculate our 'ValB' by dividing 'valA' by two and multiplying the
    # value by our constant
    valB = (valA / 2) * constant
    # We then append our new variables of 'ValA' and 'ValB' to our empty
    # catchment lists
    listA.append(valA)
    listB.append(valB)

# We can return multiple items (i.e Lists) out of Python by simply putting
# a comma between them
OUT = listA, listB
6 Likes