Python Can';在函数中找不到动量

Python Can';在函数中找不到动量,python,python-2.7,function,vpython,Python,Python 2.7,Function,Vpython,我一直在试图找出为什么Momenta函数找不到Momenta这个名字,尽管我用括号把它连起来了。输入所有输入后 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: 50 Please

我一直在试图找出为什么Momenta函数找不到Momenta这个名字,尽管我用括号把它连起来了。输入所有输入后

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: 50
Please enter the Mass of the Star: 500000000000
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: 500
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: 50
Please enter the Mass of the Star: 500000000000
Please enter the X speed of Star: 0
Please enter the Y speed of Star: 0
Please enter the Z speed of Star: 0
这就是我得到的错误

回溯(最近一次调用):文件“C:/Users/Benjamin” Andeson/Desktop/VPython工作/行星轨道版本6.py”,第93行 动量(动量、质量)名称错误:未定义名称“动量”


Momentum
ListToArray
函数中的局部变量。您可能希望坚持使用
global
语句。
Momentum
未在其使用的范围内定义。您需要从相关函数中返回值(如果可以避免,个人不建议使用
global
,您可能需要查看
class
es)。我是否可以在ListToArray函数的末尾添加动量(动量、质量)呢,如果不是,为什么它不工作
动量
列表阵列
函数中的局部变量。您可能希望坚持使用
global
语句。
Momentum
未在其使用的范围内定义。您需要从相关函数中返回值(如果可以避免,个人不建议使用
global
,您可能需要查看
class
es)。我是否可以将动量(动量、质量)添加到ListToArray函数的末尾,如果不可以,为什么它不起作用
from visual import *
from time import clock
from random import random

# Stars interacting gravitationally
# Program uses numpy arrays for high speed computations


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

Stars = []
colors = [color.red, color.green, color.blue,
          color.yellow, color.cyan, color.magenta]
PositionList = []
MomentumList = []
MassList = []
RadiusList = []


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 ListToArray(PositionList, MomentumList, MassList, RadiusList):
    pos = array(PositionList)
    Momentum = array(MomentumList)
    Masses = array(MassList)
    Masses.shape = (Nstars,1) # Numeric Python: (1 by Nstars) vs. (Nstars by 1)
    Radii = array(RadiusList)

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 Forces():
    # 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
    F = G*Mass*Mass[:,newaxis]*r/rmag[:,:,newaxis]**3 # all force pairs

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

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


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ACUTAL PROGRAM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#

for i in range(Nstars):
    DataCollection()

ListToArray(PositionList, MomentumList, MassList, RadiusList)
Momenta(Momentum, Masses)

while True:

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

    for n in range(NStars):
        F[n,n] = 0 # No self forces

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

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


#~~~~~~~~~~~~~~~~~~~~~~~~~~~ NEEDS TO BE FIXED ~~~~~~~~~~~~~~~~~~~~~~~~~~~#

# If any collisions took place, merge those stars
#   for ij in hitlist:
#       i, j = divmod(ij,Nstars) # decode star pair
#       if not Stars[i].visible: continue
#       if not Stars[j].visible: continue
#       m[i] is a one-element list, e.g. [6e30]
#       m[i,0] is an ordinary number, e.g. 6e30
#       newpos = (pos[i]*m[i,0]+pos[j]*m[j,0])/(m[i,0]+m[j,0])
#       newmass = m[i,0]+m[j,0]
#       newp = p[i]+p[j]
#       newradius = Rsun*((newmass/Msun)**(1./3.))
#       iset, jset = i, j
#       if radius[j] > radius[i]:
#           iset, jset = j, i
#       Stars[iset].radius = newradius
#       m[iset,0] = newmass
#       pos[iset] = newpos
#       p[iset] = newp
#       Stars[jset].trail_object.visible = False
#       Stars[jset].visible = 0
#       p[jset] = vector(0,0,0)
#       m[jset,0] = Msun*1E-30  # give it a tiny mass
#       Nhits = Nhits+1
#       pos[jset] = (10.*L*Nhits, 0, 0) # put it far away

#   Nsteps += 1