Error in Python: missing 1 required positional argument

9.7k views Asked by At

I'm not so good with python and I keep getting this error:

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

And this is my code:

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))

I don't really understand what is the problem, can someone help me?

2

There are 2 answers

0
Yash Talaiche On

You are not passing all the arguments value when you call you function "demand_curve". Your function "demand_curve(c,Q,y,pb)" required 4 positional arguments but you give only 3 at "demand_curve(Q_num, 50, 2)".

3
Xiidref On

When you declare your function you do this :

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

So you have four parameters c, Q, y and pb, later in the code you call it using :

demand_curve(Q_num, 50, 2)

So in the way you call it you have

  • c = Q_num
  • Q = 50
  • y = 2
  • pb = Nothing at all

And python don't like this so you should provide and additional value when you call this function or provide a default value for the last parameter for example :

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