Learning Python in Dynamo, trying to get an array from a while loop

I’m trying to learn python for a bit to improve my Dynamo skills.
I’ve (finally) managed to get a while loop working:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

s=0
v=0
a=IN[0]
r=IN[1]
t=0
tmax=IN[2]

while t<tmax:
	t=t+1
	v=(v+a)-(v*v)/a*r
	s=s+v

OUT = [v,s]

If it wasn’t clear already I’m trying to simulate movement with air resistance (not with a “real” formula but this ones doesn’t break). Where s is the distance, v the speed, a the acceleration, r, uhm, an “air resistance variable”, t time, and tmax the maximum time.

I’m currently getting the the time and distance from the output and this works well.

But what I’d like to get is arrays that would show the speed and distance at each point in time, but I don’t really understand yet how to deal with arrays yet in this context, it certainly doesn’t seem to work like it does in the Dynamo code.

Thanks in advance!

Remember I’m just doing this as practice so it’s fine if it doesn’t really work in the end, but I’d like to get som understanding on how to work with arrays.

You need to append the value at each iteration, first define your lists before the loop then use listname.append(value) inside your loop :slight_smile:

If I understand what you’re saying I’ve already tried that but it didn’t work.

I already tried putting

pos[t]=s

In the formula but I have a feeling that’s not the way to do it. I don’t even know how to input an array in there.

You don’t :wink:

What I say is this:

OR

2 Likes

That works!

I wasn’t aware of the append function so…

1 Like

The append function is very useful when wishing to record steps inside of a loop :slight_smile:

1 Like

I was trying to attach specific values to specific spots in the array. Hence the s_list[t]=s.
Does the append function just add to the current array?

Append simply adds to the end of the current array, yes :slight_smile: It’s the most common way of building lists in Python.

2 Likes