What's new

Loop how to

Oldbelt

Alibre Super User
20 years programming in Delphi Pascal dosn't help when I now write my first Phython script.
I can't make a loop I have studied a lot of "help" items.
In Delphi the ansvear is .
for n := 1 to AntPLan do
begin
__
__
end.

My code :
SupFinCode.JPG
Please give me an advice.
 

idslk

Alibre Super User
Hello oldbelt,

use some kind of:
for n in range(start,end,step)
like:
Code:
for n in range(0, AntPlan, 1):
  print n,
results in
0 1 2 3 4 5 6 7 8 9

Hint: If your XYPlane is a standard reference plane, you can use SupFin.XYPlane instead of XYPlane = SupFin.GetPlane('XY-Plane') and your skript is language independent ...
see also:
AddExtrudeBoss is erroring out.

Regards
Stefan
 

Oldbelt

Alibre Super User
Thank you Stefan.
About the plane, I need AntPlan's , thats why I use the showed code.
I have tryed something as the code you succest, but it dosn't function.

Its code at line 30 to 60 I want to loop AntPlan passes.
<for n in range(start, end, step)>
What does step means.
Regards Bent
 

idslk

Alibre Super User
Hello Bent,

if you want to loop like for n := 1 to AntPLan do it is translated to Python for n in range(AntPlan):
Then you have to indent the lines you will to loop. The indention is the same as the block between begin and end.
I don't know from where you get the value for AntPlan. If it is not set before loop, it will be zero and your loop will be very short ...
The meaning of start,end,step is, you can start a loop at e.g. Start=2, end the loop at End=10 with a distance from number to number Step=2 written: for n in range(2,10,2) which will result in 2, 4, 6, 8.
No 0 because the loop starts at 2, no 10, because 10 exceeds the range and the distances between each number is 2.
copy the following code into a new skript and press run ...
Code:
print range(2,10,2),
AntPlan = 3
Loop = [5]
for n in range(AntPlan):
  print 'n=' , n
  for m in range(Loop[0]):
    print 'm=' , m ,
    if m == 4:
      print '\n'

Hope it help's
if not, don't hesitate to ask again.

Regards
Stefan
 

Oldbelt

Alibre Super User
Hej Stefan.
I read a datafile in code line 17, this file have the needed info.
AntPLan is read from the datafile in the colapsed block in code line 23.
I try out your code and will study it further.
I have realised there is no "end" in a loop it is controlled by the indent, strange! to me, but I will learn it.
So Ihave to make indentions betwen my code line 30 og 63

For the time being tank you for your ansvar.
Regards Bent
 

Oldbelt

Alibre Super User
Hello Stefan.
Your advice was perfect, now the code run and I get Sketches in Alibre,
I run the loop twice (twoPlanes) as test, in second run the sketch 2 contain two splines :
the first and the second.
The problem siems to be the list <Temp> isn't emty at the secondt run of the loop.
<Temp> contains the x,y values for a BSpline.
I have seeked at Stackowerflow to find a simpel aproach, but people want sort and do other cracy things with list.
Do you have a hint to "0" a list so it can be reused.??
 

idslk

Alibre Super User
Hello Bent,

makes me happy to hear that your script runs.
To clear a list the simplest way is: Temp=[]
Following only a little script on lists ...
Code:
# list filled with what my fingers reached ;-)
List1 = []
List2 = [3,7,8,5,44,567,222,4,678]
List3 = []

# init a list with 9 values
for i in range(9):
  List1.append(i)
print 'List1',List1,'has now',len(List1),'items.'

# clear a list by removing n-times the last item of the list
print 'List2',List2,'has',len(List2),'items'
for n in range(len(List2)):
  List3.append(List2.pop())
  print List2, 'removed the last item'
print 'List2',List2,'is empty now.','\n','The items are copyied to List3 in reverse order.'

# clear a list with a simple empty list
print 'List3',List3,'has now',len(List3),'items.'
List3 = []
print 'List3',List3,'is empty now.'

Regards
Stefan
 
Top