尽管输入了正确的数字,但获得的参数数量无效

问题描述

import numpy as np

class Body(object): # Class for planets / moons / asteroids
    
    def __init__(self,mass,veLocity,position):
        
        self.mass = mass  # Defining variables
        self.veLocity = veLocity
        self.position = position
        
        
    def move(self,force,t):
        self.veLocity = np.add(self.veLocity + force * t / self.mass) # Code to move with time t
        self.position = np.add(self.position + self.veLocity * t)
        return self
        
def main():
    
    mars = Body(6.4185*10**23,np.array([0,0]),0]))
    mars.move(np.array([10,10]),10) # Force defined to be [10,10] and time 10
    
    
main()

错误

Traceback (most recent call last):
  File "c:\Users\p\Documents\dev\sodump\npadd.py",line 23,in <module>
    main()
  File "c:\Users\p\Documents\dev\sodump\npadd.py",line 20,in main
    mars.move(np.array([10,10] and time 10
  File "c:\Users\p\Documents\dev\sodump\npadd.py",line 13,in move
    self.veLocity = np.add(self.veLocity + force * t / self.mass) # Code to move with time t
ValueError: invalid number of arguments

解决方法

错误是 self.velocity = np.add(self.velocity + force * t / self.mass)。您只传递 1 个参数,而需要传递 2 个参数。见https://numpy.org/doc/stable/reference/generated/numpy.add.html