【问题标题】:Getting invalid number of arguments despite inputting the correct number尽管输入了正确的数字,但获得的参数数量无效
【发布时间】:2021-02-22 10:56:13
【问题描述】:
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]), np.array([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]), 10) # Force defined to be [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

【问题讨论】:

  • 请同时添加错误堆栈跟踪
  • 您的意思可能是np.add(self.position, self.velocity * t) 而不是np.add(self.position + self.velocity * t)?该函数接受两个参数并将它们相加。您正在使用常规的 + 运算符进行添加。 Here'snp.add 文档
  • 看起来 np.add 有两个参数,但您只提供了一个。
  • 此外,鉴于+ 运算符可用,您可能想写self.position = self.position + self.velocity * t 甚至self.position += self.velocity * t;比np.add(...) 符号更容易阅读...
  • 谢谢保罗,按预期工作,掌心

标签: python invalid-argument


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    • 2022-01-22
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    相关资源
    最近更新 更多