 # 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 for my python formula. I have also a IN  which is a constant in the hole process.
I actually want that every z= IN  + the result of “y” in every cicle
so for the first cycle there is actually no y so it would be z= IN 
for the second cycle it would be z=IN +y0
for the third one it would be z= IN +y0+y1
for the fourth cycle" it would be z= IN +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

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

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 + “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
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 + “y0”+“y1” y1= the result of “y” which i got at the first cycle
“y2”= (“z1”/3)*IN
for the third cycle “z2”=IN + “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 , by the second iteration i should start to add the first gotten “y” and so on

That is saved in the x and y.

Next it would read z = z + IN + n

give me a second to illustrate 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 @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 glad you solved it

Hello Pablo!

Awesome solution , 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 and IN 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 + 1)
# We also define a new variable for our constant
constant = IN

# 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