For loop problem in design script


#1

Hi all,

I have a small problem. I’m trying to understand all the IF-statement, while-loop, def methode, etc. and also the for-loop.
But I’m running into a problem there.


On the right side I have it working in Python but on the left side the Design script it’s not working…why?

I don’t get an error from Dynamo but the returned value is “null”!
Can someone help me and point out were my typo is please??

With kind regards,
Mike


#2

return = [Imperative] is used only within a function.
Since you aren’t defining a function, you should use some other variable as return is a reserved word
Try …

r = [Imperative]
{

}

You can even avoid the variable…

 [Imperative]
 {

 }

#3

Hi @Vikram_Subbaiah,

that indeed worked. Atleast I have a output now: 2800
But that is only one item from a list of 4 items.
Can you spot the error that is causing the return of only 1 item insteed of 4?

I can’t see the error becaus eI’m trying a for loop for the first time to try and understand it.
The reason for the loop is this: For every item (radi) in the list (radius) the code must check if it is equal to the word “max” and if so follow this formula and if not just pass the item (radi).


#4

The equivalent of Append in DesignScript is List.AddItemToEnd

radius = {"2000","max","1600","max"};
cover_side = 50;
outer_diameter = 17000;

[Imperative]
{
	b = {};
	for (radi in radius)
	{
		if (radi == "max")
		{
			b = List.AddItemToEnd((outer_diameter/2)-cover_side,b);
		}
		else
		{
			b = List.AddItemToEnd(radi,b);
		}
	}
	return = b;
};

However, the better approach would be …

radius = {"2000","max","1600","max"};
cover_side = 50;
outer_diameter = 17000;

[Imperative]
{
	a = 0;
	b = {};
	for (radi in radius)
	{
		if (radi == "max")
		{
			b[a] = (outer_diameter/2)-cover_side;
			a = a + 1;
		}
		else
		{
			b[a] = radi;
			a = a + 1;
		}
	}
	return = b;
};

#5

OH CRAP!!!
And I thought a for loop was a simple thing! :tired_face::tired_face:

But thanks for helping me out here Vikram!
I’m going to copy there and put them next to my other statements … and for the rest of the day I’m going and try to figure out what you’ve written down and why.

Once again: Thanks for helping me out here! :+1::+1::+1::+1::+1:

With kind regards<
Mike