2016-11-10 21 views
-1
Traceback (most recent call last): 
    File "N:\Starry\Planetary Orbits Ver 8.py", line 121 
    UpdateDisplay(pos) # Updates the 3D displays position of Masses 
    File "N:\Starry\Planetary Orbits Ver 8.py", line 100, in UpdateDisplay 
    Stars[i].pos = pos[i] 
IndexError: list index out of range 

このエラーが発生しましたが、なぜ、Numpy ArraysとListsに値があるのか​​チェックしました"IndexError:リストインデックスが範囲外です"惑星シミュレータの場合

Number of stars: 2 
Please enter The x position of the Star: 0 
Please enter The y position of the Star: 0 
Please enter The z position of the Star: 0 
Please enter the Radius of the Star: 10 
Please enter the Mass of the Star: 5000000 
Please enter the X speed of Star: 0 
Please enter the Y speed of Star: 0 
Please enter the Z speed of Star: 0 
Please enter The x position of the Star: 100 
Please enter The y position of the Star: 0 
Please enter The z position of the Star: 0 
Please enter the Radius of the Star: 10 
Please enter the Mass of the Star: 5000000 
Please enter the X speed of Star: 0 
Please enter the Y speed of Star: 0 
Please enter the Z speed of Star: 0 
[(0, 0, 0), (100, 0, 0)] # Printed PositionList # 
[(0, 0, 0), (0, 0, 0)] # Printed MomentumList # 
[5000000, 5000000]  # Printed MassList # 
[10, 10]     # Printed RadiusList # 
[[ 0 0 0]   # Printed Pos Array# 
[100 0 0]] 
[[0 0 0]     # Printed Momentum Array # 
[0 0 0]] 
[[5000000]    # Printed Masses Array # 
[5000000]] 
[10 10]     # Printed Radii Array # 

これらの値は、アイテムはリストおよびアレイに行く示すので、確かに、このコードは、配列内のアイテムを選択する:、Pythonシェルに下記のように?ここで

for i in range(Nstars): 
    UpdateDisplay(pos) 

参照のための完全なプログラムです:

Nstars = int(input("Number of stars: ")) # change this to have more or fewer stars 
G = 6.7e-11 # Universal gravitational constant 

# Typical values 
Msun = 2E30 
Rsun = 2E9 
vsun = 0.8*sqrt(G*Msun/Rsun) 
dt = 10.0 #Reprensents the change in time 
Nsteps = 0 
time = clock() 
Nhits = 0 

#~~~~~~~~~~~~~~~~~~~~~~~~~~~ Global Data ~~~~~~~~~~~~~~~~~~~~~~~~~~~# 

Stars = [] 
colors = [color.red, color.green, color.blue, 
      color.yellow, color.cyan, color.magenta] 
PositionList = [] 
MomentumList = [] 
MassList = [] 
RadiusList = [] 
pos = None 
Radii = None 
Masses = None 
F = None 
Momentum = None 

#~~~~~~~~~~~~~~~~~~~~~~~~~~~ Global Data ~~~~~~~~~~~~~~~~~~~~~~~~~~~# 

def Calculations(SpeedX, SpeedY, SpeedZ, x, y, z, Mass, Radius): 
    px = Mass*(SpeedX) 
    py = Mass*(SpeedY) 
    pz = Mass*(SpeedZ) 
    PositionList.append((x,y,z)) 
    MomentumList.append((px,py,pz)) 
    MassList.append(Mass) 
    RadiusList.append(Radius) 

def StarCreation(Stars,x,y,z,Radius): 
    Stars = Stars+[sphere(pos=(x,y,z), radius=Radius, color=colors[i % 6], 
        make_trail=True, interval=10)] 


def DataCollection(): 
    x = input("Please enter The x position of the Star: ") 
    y = input("Please enter The y position of the Star: ") 
    z = input("Please enter The z position of the Star: ") 
    Radius = input("Please enter the Radius of the Star: ") 
    StarCreation(Stars,x,y,z,Radius) 
    Mass = input("Please enter the Mass of the Star: ") 
    SpeedX = input("Please enter the X speed of Star: ") 
    SpeedY = input("Please enter the Y speed of Star: ") 
    SpeedZ = input("Please enter the Z speed of Star: ") 
    Calculations(SpeedX, SpeedY, SpeedZ, x, y, z, Mass, Radius) 

def Momenta(Momentum, Masses): 
    vcm = sum(Momentum)/sum(Masses) # velocity of center of mass 
    Momentum = Momentum-Masses*vcm # make total initial momentum equal zero 

def ListToArray(PositionList, MomentumList, MassList, RadiusList): 
    global pos 
    pos = array(PositionList) 
    global Momentum 
    Momentum = array(MomentumList) 
    global Masses 
    Masses = array(MassList) 
    Masses.shape = (Nstars,1) # Numeric Python: (1 by Nstars) vs. (Nstars by 1) 
    global Radii 
    Radii = array(RadiusList) 
    Momenta(Momentum, Masses) 

def Forces(pos, Radii, Masses): 
    # Compute all forces on all stars 
    r = pos-pos[:,newaxis] # all pairs of star-to-star vectors (Where r is the Relative Position Vector 
    for n in range(Nstars): 
     r[n,n] = 1e6 # otherwise the self-forces are infinite 
    rmag = sqrt(sum(square(r),-1)) # star-to-star scalar distances 
    hit = less_equal(rmag,Radii+Radii[:,newaxis])-identity(Nstars) 
    hitlist = sort(nonzero(hit.flat)[0]).tolist() # 1,2 encoded as 1*Nstars+2 
    global F 
    F = G*Masses*Masses[:,newaxis]*r/rmag[:,:,newaxis]**3 # all force pairs 

def SelfForces(F): 
    F[n,n] = 0 

def UpdateMomentaPositions(Momentum, pos, Masses): 
    Momentum = Momentum+sum(F,1)*dt   
    pos = pos+(Momentum/Masses)*dt 

def UpdateDisplay(pos): 
    Stars[i].pos = pos[i] 


#~~~~~~~~~~~~~~~~~~~~~~~~~ Actual Proagram ~~~~~~~~~~~~~~~~~~~~~~~~~# 

for i in range(Nstars): 
    DataCollection() 

ListToArray(PositionList, MomentumList, MassList, RadiusList) 
print (PositionList) 
print (MomentumList) 
print (MassList) 
print (RadiusList) 
print (pos) 
print (Momentum) 
print (Masses) 
print (Radii) 

while True: 

    rate(100) # No more than 100 loops per second on fast computers 
    Forces(pos, Radii, Masses)# Computes all the forces between masses 

    for n in range(Nstars): 
     SelfForces(F) # No self forces 

    UpdateMomentaPositions(Momentum, pos, Masses) # Updates the Momentum and Positions of Stars 

    for i in range(Nstars): 
     UpdateDisplay(pos) # Updates the 3D displays position of Masses 

答えて

2

あなたの問題は、あなたがStarCreation関数を呼び出すときに、実際にStarsに任意の星を追加することはありませんということです。

  1. あなたはどちらかStars+=[sphere(...)]を介してグローバルStarsリストにインプレースを変更することができます。

    Stars = Stars+[sphere(pos=(x,y,z), radius=Radius, color=colors[i % 6], 
           make_trail=True, interval=10)] 
    

    は、あなたがその問題には2つのソリューションを持っている:あなたは、実際には、その行を持つグローバルStarsをシャドウローカルStarsリストを再割り当てまたはStars.append(sphere(...))

  2. それともスターは、グローバル変数であり、それはこのように、陰にすべきではないということのpythonを伝える:

def StarCreation(Stars,x,y,z,Radius): 
    global Stars 
    Stars = Stars+[sphere(pos=(x,y,z), radius=Radius, color=colors[i % 6], 
       make_trail=True, interval=10)] 

編集:私が想定した間違いは、申し訳ありませんでした。

+0

おかげでとにかく、私はこの問題は、それは次のようにする必要がありますので、私は機能で私を呼んでdidntのことだったと思います。私レンジ(Nstars)で用: UpdateDisplayのは問題ではありませんでした(POS、ⅰ) –

+0

@BenjaminAndersonとそれが引き起こしていることができませんでした(私はあなたの修正がはるかに良いですが、元のコードはPythonのための問題ではありませんでした意味)あなたが言ったエラー。定義されていない変数を使用しようとすると、 'IndexError'ではなく' NameError'が発生します。 – jadsq

関連する問題