我不太擅长 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):
...
因此,您有四个参数 c
、Q
、y
和 pb
,稍后在您调用的代码中它使用:
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/