Help with Python UnboundLocalError: local variable referenced before assignment

后端 未结 4 1761
Happy的楠姐
Happy的楠姐 2021-01-14 23:53

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

4条回答
  •  旧巷少年郎
    2021-01-15 00:37

    On line 39 you do

    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. 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. But since you had vs in the left hand side, you created an uninitialized local variable.

    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 :)

提交回复
热议问题