名称错误。无法将变量赋给数组

时间:2014-07-21 19:51:06

标签: python arrays

在readlines()循环中,下面显示的部分显示错误:

Traceback (most recent call last):
  File "test.py", line 56, in <module>
    print("%s,%s are {:5.16f}, {:5.16f}"%(atom2,atom3)).format(Nj,Nk)
NameError: name 'Nj' is not defined


            if i>7:
                    dummy=line.strip().split()
                    j=i-7
                    Njx=dummy[0]
                    Njy=dummy[1]
                    Njz=dummy[2]

                    import string
                    if j==2:
                      Njx=string.atof(Njx)
                      Njy=string.atof(Njy)
                      Njz=string.atof(Njz)
                      Nj = [Njx, Njy, Njz]

                    if j==3:
                      Nk = [string.atof(Njx),string.atof(Njy),string.atof(Njz)]

                    print("%s,%s are {:5.16f}, {:5.16f}"%(atom2,atom3)).format(Nj,Nk)

1 个答案:

答案 0 :(得分:3)

如果j == 2,您只定义Nj。如果j不等于3,则可能与Nk具有相同的错误。尝试在if j == 2之前先声明这两个值。

Nj = [];
Nk = [];

if j==2:
  ...