Create a simple for loop

Robots.dyn (22.4 KB)
2019-03-18_08h22_40 2019-03-18_08h23_02


I do again some small stuff arround “coding” I need some help:

I have two lists and i will combine them, but I create allways a endless-loop.
When i use the comment “break” the whole loop doesn`t work.

Does anybody have some insperation and variations for me :slight_smile: ?



So your code is:

robots = IN[0]
colours = IN[1]
index = 0

index = index + 1

for i in robots:

OUT = "Ich bin " + robots[index] + " und " + colours[index]

To start with, the infinite loop comes from the line robots.append(i) which basically means you are adding a new item into the robots list, while also iterating through the list. Thus, you are infinitely adding and reading the same list. I think what you meant to do here is create a list of indices that has the same length as robots. In which case, you could remove the entire for loop and just use this:
index = range(len(robots)) which creates a list from 0 to the length of robots. In codeblock terms, this would be index = 0..List.Count(robots)-1 (For codeblocks, the -1 is important because you want the same amount, or else you get one item more than the length).

Next, your out statement, you want to iterate through the robots and colour list using the index list. Here you would need a for loop for i in index:. But OUT needs to be a list you can append to so you would need to write OUT = [] somewhere before the for loop and then OUT.append(your statement here) inside the for loop. Overall it would look like this:

robots = IN[0]
colours = IN[1]
index = range(len(robots))

OUT = []
for i in index:
    OUT.append("Ich bin " + robots[i] + " und " + colours[i])

Now a fun variation using the formatting of strings available in python:

robots = IN[0]
colours = IN[1]
OUT =["Ich bin %s und %s" % (r, c) for r,c in zip(robots, colours)]


I guess technically the newer way for formating strings is this:
OUT =["Ich bin {} und {}".format(r, c) for r,c in zip(IN[0], IN[1])]


Thats a good info. I ve never thought to put loops direct in the output