简体   繁体   English

"帮助 Python UnboundLocalError:赋值前引用的局部变量"

[英]Help with Python UnboundLocalError: local variable referenced before assignment

I have post the similar question before,however,I think I may have misinterpreted my question,so may I just post my origin code here,and looking for someone can help me,I am really stuck now..thanks alot.我以前发布过类似的问题,但是,我想我可能误解了我的问题,所以我可以在这里发布我的原始代码,并寻找可以帮助我的人,我现在真的被困住了..非常感谢。

from numpy import *
import math as M

#initial condition  All in SI unit
G=6.673*10**-11   #Gravitational constant
ms=1.9889*10**30 #mass of the sun
me=5.9742*10**24 #mass of the earth
dt=10           #time step
#Creat arrays
vs=array([[0,0,0]])     #1st element stand for x component of V of earth
ve=array([[29770,0,0]])
rs=array([[0,0,0]])           
re=array([[0,1.4960*10**11,0]])

#First update velocity in order to start leapfrog approximation
fs=-G*ms*me*((rs-re)/(M.sqrt((rs-re)[0][0]**2+(rs-re)[0][1]**2+(rs-re)[0][2]**2))**3)
fe=-fs
vs=vs+fs*dt/ms 
ve=ve+fe*dt/me

n=input('please enter the number of timestep you want it evolve:')
#update force
def force(n,ms,me,rs,re,G):
    rs,re=update_r(rs,re,n,dt)
    fs=-G*ms*me*((rs-re)/(M.sqrt((rs-re)[0][0]**2+(rs-re)[0][1]**2+(rs-re)[0][2]**2))**3)
    fe=-fs
    return fs,fe

#update velocities
def update_v(n,vs,ve,ms,me,dt,fs,fe):
    fs,fe=force(n,ms,me,rs,re,G)
    i=arange(n)
    vs=vs+fs[:]*i[:,newaxis]*dt/ms
    ve=ve+fe[:]*i[:,newaxis]*dt/me
    return vs,ve

#update position
def update_r(rs,re,n,dt):
    vs,ve=update_v(n,vs,ve,ms,me,dt,fs,fe)
    i=arange(n)
    rs=rs+vs[:]*i[:,newaxis]*dt
    re=re+ve[:]*i[:,newaxis]*dt
    return rs,re
#there is start position,v,r,f all have initial arrays(when n=0).
#then it should calculate f(n=1) then use this to update v(n=0)
#to v(n=1),then use v(n=1) update r(n=0) to r(n=1),then use r(n=1)
#update f(n=1) to f(n=2)....and so on until finish n.but this code seems doesnt do this,,how can I make it? – 

where do you call force in this code? 在这段代码中你在哪里调用force?

In any event, the problem is in update_r. 无论如何,问题出在update_r中。 You reference vs in the first line of update_r even though vs is not defined in this function. 您在update_r的第一行中引用vs,即使此函数中未定义vs。 Python is not looking at the vs defined above. Python没有关注上面定义的vs。 Try adding 尝试添加

global vs

as the first line of update_r or adding vs to the parameter list for update_r 作为update_r的第一行或将update添加到update_r的参数列表中

In the first line of update_r , you have vs,ve=update_v(n,vs,ve,ms,me,dt,fs,fe) . update_r的第一行,你有vs,ve=update_v(n,vs,ve,ms,me,dt,fs,fe) Look at the function that you are calling. 查看您正在调用的函数。 You are calling update_v with a bunch of parameters. 您正在使用一堆参数调用update_v One of these parameters is vs . 其中一个参数是vs However, that is the first time in that function that vs appears. 但是,这是vs出现的第一次该功能。 The variable vs does not have a value associated with it yet. 变量vs还没有与之关联的值。 Try initializing it first, and your error should disappear 尝试首先初始化它,你的错误应该消失

Put an additional global statement containing all your globals after each def statement. 把一个附加的global包含每个毕竟您的全局声明def声明。 Otherwise, all globals are transformed into locals within your def without it. 否则,所有全局变量都会转换为def中的本地变量而不是它。

def update_v(n,vs,ve,ms,me,dt,fs,fe):
    global vs, ve, ...  

On line 39 you do 在第39行你做

vs,ve=update_v(n,vs,ve,ms,me,dt,fs,fe)

while you are inside a function. 而你在一个功能。 Since you defined a global variable called vs , you would expect this to work. 由于您定义了一个名为vs全局变量,因此您可以期望这样做。 It would have worked if you had: 如果你有:

vs_new,ve_new = update_v(n,vs,ve,ms,me,dt,fs,fe)

because then the interpreter knows vs in the function arguments is the global one. 因为那时解释器在函数参数中知道vs是全局参数。 But since you had vs in the left hand side, you created an uninitialized local variable. 但是由于你在左侧有vs ,你创建了一个未初始化的局部变量。

But dude, you have a much bigger problem in your code : update_r calls update_v, update_v calls force, and force calls update_r - you will get a stack overflow :) 但是老兄, 你的代码中有一个更大的问题 :update_r调用update_v,update_v调用force,强制调用update_r - 你会得到堆栈溢出 :)

I got that error when my class name was assigned to a variable that is called exactly like its name.当我的类名被分配给一个与其名称完全相同的变量时,我得到了这个错误。 example ClassName = ClassName.<\/code>例如ClassName = ClassName.<\/code> You may do this if you come from .Net如果您来自 .Net,您可以这样做

"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:UnboundLocalError帮助:分配前引用了局部变量 - Python: Help with UnboundLocalError: local variable referenced before assignment UnBoundLocalError:赋值之前引用的局部变量(Python) - UnBoundLocalError: local variable referenced before assignment (Python) python - UnboundLocalError:分配前引用的局部变量 - python - UnboundLocalError: local variable referenced before assignment Python-UnboundLocalError:分配前引用的局部变量 - Python - UnboundLocalError: local variable referenced before assignment` Python UnboundLocalError:分配前引用了局部变量 - Python UnboundLocalError: local variable referenced before assignment Python | 如果变量:| UnboundLocalError:赋值前引用的局部变量&#39;variable&#39; - Python | if variable: | UnboundLocalError: local variable 'variable' referenced before assignment UnboundLocalError:在python闭包中赋值之前引用的局部变量 - UnboundLocalError: local variable referenced before assignment in python closure Python(3.3):UnboundLocalError:分配前已引用局部变量 - Python (3.3): UnboundLocalError: local variable referenced before assignment Python 分裂问题 UnboundLocalError:分配前引用了局部变量“e” - Python Splinter issue UnboundLocalError: local variable 'e' referenced before assignment UnboundLocalError:分配前引用的局部变量“转” - python - UnboundLocalError: local variable 'turn' referenced before assignment - python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM