-1

我正在尝试制作一个程序来模拟运动。这是代码

class Object:
    forc={}
    srtpos = 0
    vel = 0
    def __init__(self,mass, srtpos = 0):
        self.mass = mass
        self.srtpos = 0
    def UpVel(vel, time = 1, a = 0):
        vel1 = vel + a*t
        vel = vel1
    def Move(self, vel, time = 1, a = 0):
        self.srtpos+= self.vel*time +(0.5)*a*(time**2)
        UpVel(self.vel, time, a)
a = Object(5)
print(a.srtpos)
for i in range(5):
    a.Move(5)
    print(a.srtpos)

UpVel()功能正在更新速度。当我尝试通过该Move()函数运行它时,程序给了我错误

Traceback (most recent call last):
  File "/media/atharva/DATA/Python/PhySim.py", line 17, in <module>
    a.Move(5)
  File "/media/atharva/DATA/Python/PhySim.py", line 13, in Move
    UpVel(self.vel, time, a)
NameError: name 'UpVel' is not defined

我已经尝试将函数调用放入__main__并更改函数的名称。

任何帮助表示赞赏。提前致谢。

4

1 回答 1

0

尝试使用

self.UpVel(self.vel, time, a)

如果你想在没有自我的情况下使用它。它需要是一个静态方法。

于 2021-08-17T16:24:31.770 回答