-1

I am trying to make a program to simulate movement. This is the code

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)

The UpVel() function is updating the velocity. When i try to run this through the Move() function, the program is giving me error

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

I have tried putting the function call in __main__ and also changing the name of the function.

Any help appreciated. Thanks in advance.

  • You need to add an explicit ```self``` parameter **at the beginning** of every method of a class –  Aug 17 '21 at 16:21
  • This isn't [tag:C++], you have to be explicit: `self.UpVel` – Peter Wood Aug 17 '21 at 16:22
  • You need `self.UpVel` to call the method. – kaya3 Aug 17 '21 at 16:22
  • Almost duplicate of [python - How to call a function within class? - Stack Overflow](https://stackoverflow.com/questions/5615648/how-to-call-a-function-within-class) except that... – user202729 Aug 17 '21 at 16:22
  • There are more errors in the question than that. In Python parameters are passed by value, except for mutable types (it's complex). And you don't usually make that a class method. Learn Python properly. – user202729 Aug 17 '21 at 16:23

1 Answers1

0

Try to use

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

If you want to use it without self. it needs to be a static method.

Drummer 08
  • 21
  • 4