python - Python 错误 : missing 1 required positional argument

标签 python typeerror positional-argument

我不太擅长 python,并且不断收到此错误:

TypeError: demand_curve() missing 1 required positional argument: 'pb'

这是我的代码:

P,c,Q,y,pb,N,X,pf,t=sp.symbols('P c Q y pb N X pf t')

def demand_curve(c,Q,y,pb):
    demand = (c.log(Q)-(-4.507+(0.841*y)+(0.2775*pb)))/(-0.397) 
    return demand

Q_num = np.linspace(0,100,100)

fig,ax=plt.subplots()
ax.set_ylabel('P')
ax.set_xlabel('E')

ax.plot(demand_curve(Q_num, 50, 2), Q_num,label='E (a=100,b=2)')

#legend:
ax.legend(loc='upper right', frameon=False)
ax.set(xlim=(0,100))
ax.set(ylim=(0,60))

我不太明白问题出在哪里,有人可以帮助我吗?

最佳答案

当你声明你的函数时,你会这样做:

def demand_curve(c,Q,y,pb):
    ...

因此,您有四个参数 cQypb,稍后在您调用的代码中它使用:

demand_curve(Q_num, 50, 2)

所以按照你所说的方式,你已经

  • c = Q_num
  • Q = 50
  • y = 2
  • pb=什么都没有

Python 不喜欢这样,所以当你调用这个函数时你应该提供附加值,或者为最后一个参数提供默认值,例如:

def demand_curve(c,Q,y,pb = "a default value"):
    ...

关于python - Python 错误 : missing 1 required positional argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71951025/

相关文章:

python - 为什么 pip freeze list "pkg-resources==0.0.0"?

python - TypeError:只能在 Python 中连接元组(不是 "int")

python - GET() 采用 1 个位置参数,但已给出 2 个

python - 顺序 Keras 模型能否获得未展平的多维输入(例如图像)(即输入形状是每个图像的 MxN 像素)?

angular - ng serve --prod 抛出 "ERROR TypeError: (void 0) is not a function"

python - Python 3.6 sum() 是否有 `start=0` 关键字参数?

python - 如何使用 BeautifulSoup 寻找值(value)

python - Python Dask 包性能低下?

python - 提取特定字符后的 float