I've implemented a fibonacci Measurement algorithm with 2 parameter n and p.
I got this issue,
TypeError Traceback (most recent call last)
<ipython-input-19-295638b26e62> in <module>
2 N = 10
3 # [F(n,p) for n in range(N)]
----> 4 print(F(10,1))
<ipython-input-12-fda62c8ec9a6> in F(n, p)
6 elif n <= p+1:
7 return n
----> 8 return F(n-1) + F(n-p-1)
TypeError: F() missing 1 required positional argument: 'p'
I have input 2 parameters n =10, p = 1, but still having this problem "missing 1 required argument". Does anyone know why and solution for this or any suggestion would be appreciated!
There could be two potential issues.
You're calling a function, F, that doesn't seem to be defined in the snippet you've attached. You might want to change it to fibonacci_of if it is supposed to call itself recursively. In addition, since the fibonacci_of accepts two parameters, you would need to call it with two arguments
If F is already defined elsewhere, it is supposed to accept more than one argument. You could check its function definition and see the parameter requirements. See attached examples.
def square(a): # Requires single parameter.
return a ** 2
def add(a, b): # Requires two parameters.
return a + b
Related
def list_gen(a,b,c,d):
print(a,b,c,d)
l=[]
for i in range(a,b):
for j in range(c,d):
l.append(d[i,j])
return l
When I pass the arguments to the function list_gen(0,3,0,3) I am getting below error:
TypeError Traceback (most recent call last)
<ipython-input-51-ac343943d448> in <module>()
----> 1 list_gen(0,3,0,3)
<ipython-input-49-afc3d3a347a9> in list_gen(a, b, c, d)
4 for i in range(a,b):
5 for j in range(c,d):
----> 6 l.append(d[i,j])
7 return l
TypeError: 'int' object is not subscriptable
But this code works without any issues. Can anyone tell what is the error here ?
for i in range(0,3):
for j in range(0,3):
print(d[i,j])
Apparently you have a global variable d containing a dict that you want to access. But you used d as the name of a parameter of the function, so that hides the global name. When you use d[i,j], it tries to index the number 3, which is not possible.
The solution is to use a different variable for the function parameter.
def list_gen(a,b,c,e):
print(a,b,c,e)
l=[]
for i in range(a,b):
for j in range(c,e):
l.append(d[i,j])
return l
You'll run into problems like this less often if you use more verbose, meaningful variable names than d.
The parameter in range has to be integer and since the for j in range(c,d): didn't throw error, it mean d is int and you cannot reference int using d[i,j] in the line l.append(d[i,j]) . You probably have a variable named d whose scope is active during the print statement.
I'm python user and I'm evaluate combination in python.
For two integers m,n, nCm=n!/(m!*(n-m)!)
So, I defined factorial and combination function.
factorial function is working, but combination function does not working.
What is the error?
1> Factorial Function
def factorial(a):
f=1
for i in range(1,a+1):
f=f*i
print(f)
2> Combination Function
def Combination(n,m):
fn=factorial(n)
fm=factorial(m)
fnm=factorial(n-m)
ncm=factorial(n)/(factorial(m)*factorial(n-m))
print(ncm)
In factorial function, For example, factorial(4)=24 is working in python.
But, In combination function,
When I typing Combination(4,2),
24
2
2
24
2
2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-18-daae2b10838c> in <module>()
----> 1 Combination(4,2)
<ipython-input-17-76c6e425ad35> in Combination(n, m)
3 fm=factorial(m)
4 fnm=factorial(n-m)
----> 5 ncm=factorial(n)/(factorial(m)*factorial(n-m))
6 print(ncm)
7
TypeError: unsupported operand type(s) for *: 'NoneType' and 'NoneType'
What's the matter in my coding?
Instead of printing the output of a function, you should return the result.
def factorial(a):
f=1
for i in range(1,a+1):
f=f*i
return f
def combination(n,m):
return = factorial(n)/(factorial(m)*factorial(n-m))
print combination(4,2)
One more remark: after calculating factorial(n), it will be stored in your fn variable. You shouldn't calculate it again, but rather take that value and "recycle" it.
def factorial(a):
f=1
for i in range(1,a+1):
f=f*i
return f
def Combination(n,m):
fn=factorial(n)
fm=factorial(m)
fnm=factorial(n-m)
ncm=fn/(fm*fnm)
print(ncm)
Combination(4,2)
In your code there is missing return statement in factorial function.
Suppose I have the following simple function and inputs:
dates = pd.date_range('20170101',periods=20)
a1 = np.ones(3)
b1 = pd.DataFrame(np.random.randint(10,size=(20,3)),index=dates,columns=['foo','bar','see'])
def test_func(a,b):
c = (a*b).sum(axis=1)
d = c.std()*np.sqrt(3)
e = c.mean()/d
return -np.array(e)
I would like to solve this function for a that minimizes the output (maximizes e).
scipy.optimize.fmin(test_func,a1,args=(b1))
But this throws a type error
TypeError: test_func() takes 2 positional arguments but 4 were given
My quesiton is i) is this a good way to solve for the max of such a function and ii) what the devil is the problem?
You are missing a comma after the b1 in the extra argument:
scipy.optimize.fmin(test_func,a1,args=(b1,))
seems to work.
I am trying to pass arguments to a function and I think I'm doing it correctly but it still gives the error:
TypeError: p_vinet() takes 2 positional arguments but 4 were given
Here's the pieces of my code first and then I'll give the part that gives the error.
volumeMgO is a previously calculated array consisting of:
array([ 7.64798549, 7.67153344, 7.67153344, 7.8068763 , 7.97288941,
8.14781986, 8.33321177, 8.53118834, 8.74433596, 8.97545339,
9.22826581, 9.50740563, 9.81962839])
params_MgO is this:
params_MgO = [11.244, 160., 4.0]
The vinet function is:
def p_vinet(v, params):
"""
This function will calculate pressure from Vinet equation.
Parameters
==========
v = volume
params = [V0, K0, K0']
Returns
=======
Pressure calculated from Vinet equation
"""
f_v = np.power( v/params[0], 1./3.)
eta = 1.5 * (params[2] - 1.)
P = 3 * params[1] * ((1 - f_v) / np.power(f_v, 2) ) * np.exp(eta * (1 -
f_v))
return P
Finally, the slope function is just a simple way of taking a derivative:
def slope(func, x, h, args=()):
"""
find a slope for function f at point x
Parameters
=========
f = function
x = independent variable for f
h = distance along x to the neighboring points
Returns
=======
slope
"""
rise = func(x+h, *args) - func(x-h, *args)
run = 2.*h
s = rise/run
return s
Now here is where the issue comes. When I type:
BulkModulus_MgO = np.zeros(volumeMgO.size)
for i in range(volumeMgO.size):
BulkModulus_MgO[i] = slope(p_vinet, volumeMgO[i], volumeMgO[i]*0.0001,
args=(params_MgO))
I get this error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-116-60467d989bbc> in <module>()
1 BulkModulus_MgO = np.zeros(volumeMgO.size)
2 for i in range(volumeMgO.size):
----> 3 BulkModulus_MgO[i] = slope(p_vinet, volumeMgO[i],
volumeMgO[i]*0.0001, args=(params_MgO))
<ipython-input-100-618f25e85d34> in slope(func, x, h, args)
15 """
16
---> 17 rise = func(x+h, *args) - func(x-h, *args)
18 run = 2.*h
19
TypeError: p_vinet() takes 2 positional arguments but 4 were given
I don't get it. p_vinet needs arguments v and params, and I supply v through the x+h and x-h in the slope function, and the params is a list with 3 entries that p_vinet unpacks. So that's 2 arguments. Why is it saying I'm supplying 4?
Sorry if it's slightly confusing how I'm presenting the code. I'm coding in jupyter notebook and all the functions are separate. volumeMgO is calculated separately from previous code with no issues.
Let's look at this line of code:
BulkModulus_MgO[i] = slope(p_vinet, volumeMgO[i], volumeMgO[i]*0.0001,
args=(params_MgO))
args should be like this: args=(params_MgO,) (tuple with one element in it) instead of args=(params_MgO) (not tuple, just array of 3 elements) because in second case unpacking *args in slope() function gives you 3 additional arguments (each element of params_MgO). That's why you got 4 arguments in slope() function. In first case unpacking gives you entire array as single parameter (like params in p_vinet() function).
I am trying to write a Python turtle program that draws a Spirograph and I keep getting this error:
Traceback (most recent call last):
File "C:\Users\matt\Downloads\spirograph.py", line 36, in <module>
main()
File "C:\Users\matt\Downloads\spirograph.py", line 16, in main
spirograph(R,r,p,x,y)
File "C:\Users\matt\Downloads\spirograph.py", line 27, in spirograph
spirograph(p-1, x,y)
TypeError: spirograph() missing 2 required positional arguments: 'x' and 'y'
>>>
This is the code:
from turtle import *
from math import *
def main():
p= int(input("enter p"))
R=100
r=4
t=2*pi
x= (R-r)*cos(t)-(r+p)*cos((R-r)/r*t)
y= (R-r)*sin(t)-(r+p)*sin((R-r)/r*t)
spirograph(R,r,p,x,y)
def spirograph(R,r,p,x,y):
R=100
r=4
t=2*pi
x= (R-r)*cos(t)-(r+p)*cos((R-r)/r*t)
y= (R-r)*sin(t)-(r+p)*sin((R-r)/r*t)
while p<100 and p>10:
goto(x,y)
spirograph(p-1, x,y)
if p<10 or p>100:
print("invalid p value, enter value between 10 nd 100")
input("hit enter to quite")
bye()
main()
I know this maybe has a simple solution but I really can't figure out what I am doing wrong, this was an exercise in my computer science 1 class and I have no idea how to fix the error.
The last line of the traceback tells you where the problem is:
File "C:\Users\matt\Downloads\spirograph.py", line 27, in spirograph
spirograph(p-1, x,y) # <--- this is the problem line
TypeError: spirograph() missing 2 required positional arguments: 'x' and 'y'
In your code, the spirograph() function takes 5 arguments: def spirograph(R,r,p,x,y), which are R, r, p, x, y. In the line highlighted in the error message, you are only passing in three arguments p-1, x, y, and since this doesn't match what the function is expecting, Python raises an error.
I also noticed that you are overwriting some of the arguments in the body of the function:
def spirograph(R,r,p,x,y):
R=100 # this will cancel out whatever the user passes in as `R`
r=4 # same here for the value of `r`
t=2*pi
Here is a simple example of what is happening:
>>> def example(a, b, c=100):
... a = 1 # notice here I am assigning 'a'
... b = 2 # and here the value of 'b' is being overwritten
... # The value of c is set to 100 by default
... print(a,b,c)
...
>>> example(4,5) # Here I am passing in 4 for a, and 5 for b
(1, 2, 100) # but notice its not taking any effect
>>> example(9,10,11) # Here I am passing in a value for c
(1, 2, 11)
Since you always want to keep this values as the default, you can either remove these arguments from your function's signature:
def spirograph(p,x,y):
# ... the rest of your code
Or, you can give them some defaults:
def spirograph(p,x,y,R=100,r=4):
# ... the rest of your code
As this is an assigment, the rest is up to you.
The error tells you that you're using too few arguments to call spirograph
Change this code:
while p<100 and p>10:
goto(x,y)
spirograph(R,r, p-1, x,y) # pass on the missing R and r
You're not using these arguments though, but you still have to give them to the function to call it.