Unexpected Error in a For Loop

I’m facing an unexpected error when manupulating a For loop in dynamo.
In this case i = 0 … 3. and j = 0…20
It works how it should work for i = 0 to 2… but when the value of i = 3 it doesn’t.
i’m attaching the dyn file and a picture to describe it.

Thanks for your help!

image
Teste.dyn (5.0 KB)

Edit: It looks like you’re trying to set up what’s essentially a dictionary. Have you tried using Springs node Dictionary.ByKeysValues?

1 Like

it’s kind of a dictionary… but this script is part of something much larger, and in this specific case it is really necessary for me to use for loops.
But thanks for your help !

Hi @ramoon.bandeira what is not working here?

You have a syntax error in your code:

  1. In your outer loop you declare i, the accessor, to iterate a list of integers set to the count of CapacidadeDeCargas (it terminates once i == 3 in your example)
  2. In your inner loop you then increment i manually but you are iterating a larger list than CapacidadeDeCargas (terminates once j == 20), which means i is set to values higher than 3 so you are indexing beyond the bounds of this list and that’s causing the unexpected behavior

What you need to do is remove the line which manually increments i (since you’re indexing, you don’t need to), and use List.AddItemToEnd() for both brevity and because the number of items in res are indeterminate due to your if statement:
res = List.AddItemToEnd(ListaDeBitolas[j], res);

@Michael_Kirschner2 odd this doesn’t throw an exception since the indexing is occurring outside of the bounds of the array. Is that a bug?

1 Like

Well,
a friend of mine who is a computer scientist told me i shoul change the line “i = i +1” for “break;”
and it worked perfectly