0

Total newbie question here. I have been looking around here and other internet searches and cannot find a great solution. I am scripting some geometry variables in the software Ansys. It is called spaceclaim and they use IronPython for the scripting language. I cannot at this time import numpy to take advantage of any libraries. I am trying to pattern a set of points, where the points are a function of a geometric parameter. I tried to boil it down for clarity: (the numbering and equations are meaningless, just wanted to lay out an example that illustrates my problem)

i=0
S_1 = 3
S_= []
xpi_ = []
ff_= 2
np=4
for i in range(np):
    S_[i + 2] = S_[i + 1] + 10 * ff_
    xpi_[i + 2] = (S_[i + 1])**2

So in this example:

first loop

S_2 = S_1 + 10 * ff = 23

xpi_2 = S_1**2 = 9

second loop

S_3 = S_2 + 10 * ff = 43

xpi_3 = S_2**2 = 529 ...

When I execute it jupyter I get this:

----> 8. S_[i + 2] = S_[i + 1] + 10 * ff_

IndexError: list index out of range

Much appreciated for any help!

John

johnsboyd
  • 9
  • 1
  • 3
    `S_` and `xpi_` are empty lists, hence the IndexError on `S_[i+1]`. For implementing a recursive definition you'd have to initialize them each with at least 2 initial values, or a list of zeros. – Iguananaut Jul 27 '22 at 01:06
  • 3
    If your first iteration, `S_` is an empty list. `i` is `0`, so what do you thinkn `S_[i + 1]` will be? How about `S_[i + 2]`? You cannot get or set indices of a list that doesn't have those elements. To add to a list, you need to use `append`. – Pranav Hosangadi Jul 27 '22 at 01:06
  • Please include stacktrace – Rodrigo Laguna Jul 27 '22 at 01:50
  • Are you trying to dynamically generate variable names? If so, don't. See https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables. – ndc85430 Jul 27 '22 at 04:21
  • Thanks! I figured it could be some sort of initialization issue. I can’t currently load numpy whaler it appears you can use zeros to initialize. Seeing the output I am looking for is there a way to initialize the variables with a zero array without numpy. I looked around but can’t quite make sense of how to implement. Could anyone provide a suggestion? I tried: S_0 = [0]*10 – johnsboyd Jul 27 '22 at 04:28
  • I don’t think I’m trying to dynamically create variables. – johnsboyd Jul 27 '22 at 04:34

1 Answers1

0

use this code:

S_=[S_1]
for i in range(np):
    S_.append(S_[i] + 10 * ff_)
    xpi_.append((S_[i])**2)
Alireza75
  • 513
  • 1
  • 4
  • 19