0

As the title say, one variable is causing another to become zero for no apparent reason. Here is my code, shorted with a ton of print statements trying to figure out what's going on.

for i in range(1, number_of_layers):
    a_nt[i] = 0.5 * ((dia / 2) ** 2) * (theta_t[i] - sin(theta_t[i]))
    print("ant 1 ", a_nt[i])

I assign a value to a_nt and later decide how that value will be used in an if state to assign it to a_t. The code goes into the else portion.

else:
    print("ant 2 ", a_nt[i])
    a_t[i] = a_nt[i]
    print("at 1 ", a_t[i])
    y_t[i] = y_nt[i]
    print("at 2 ", a_t[i])
    theta_c[i] = 0
    print("at 3 ", a_t[i])

Python prints out:

ant 1 0.005738905060148785

ant 2 0.005738905060148785

at 1 0.005738905060148785

at 2 0.005738905060148785

at 3 0

For a reason that I cannot determine, a_t becomes zero after I assign theta_c a value of zero. I'm relatively new to python, but this is driving me insane!

1 Answers1

0

If you make an assignment like theta_c = a_t, then you're not actually copying the array, you're merely referencing the same array with another variable.

Thus, when you change theta_c, you're also changing a_t. You should be probably taking a shallow copy instead:

theta_c = list(a_t)

Of course, depending on the situation, even a shallow copy may not be sufficient, but in this case it will probably do the trick.

jmk
  • 1,938
  • 14
  • 15