cannot unpack non-iterable float object - python

Whenever I run my code I keep getting the error of "cannot unpack non-iterable float object", I'm confused on where the error is coming from, do I have to use the iterating variable in some way?
def DEADBEEF(n):
count = 0
for i in range(n):
x ,y = np.random.uniform(0,1)
if (np.sqrt(x**2 + y**2)<=1):
count = count + 1
answer = count/100
return answer
holder = DEADBEEF(100)

np.random.uniform returns a single float as long as you don't pass the size parameter.
If you want to use x, y = ..., you must supply at least two values on the right side of the assignment.
If you want to assign a float to both x and y using np.random.uniform, try using the size parameter:
x, y = np.random.uniform(0, 1, size=2)

Related

Project Euler Problem 2 in Python with Error Message

I am a complete beginner in coding and was trying problems from Project Euler in Python. Could anyone please explain what is wrong with my code?
Problem: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
a = []
x = 1
y = 2
for y in range (1, 400000):
if x % 2:
a.append(x)
x = y, y = x + y
print(sum(a))
I received the error message "cannot unpack non-iterable int object". What does this message mean and how to avoid it?
You get that error because you have the line x = y, y = x + y which is interpreted as an attempt to unpack values from an iterable into y, y, so you need to have a new line between x = y and y = x + y.
However since x is assigned with the value of y before y is updated, the better option is to change the line to x, y = y, x + yas JackTheCrab suggested in a comment.
Look the below example as well to explain the error:
price, amount = ["10$", 15] #iterable that can be unpacked
price, amount = 15 # this is just an int and this row will throw error "Cannot unpack non-iterable int object"
x = y, y = x + y
When i take away the addition from the end and the assignment from the beginning, the error still is there.
y, y = x
Output : an error. about non-iterable int ! Because you can unpack an iterable.
iterable = [0, 1, 2]
zero = iterable[0]
one = iterable[1]
two = iterable[2]
is not very good (sorry for my bad english ...) to use, because you have to type iterable[n] again and again. That's why you can unpack such iterables like this:
iterable = [0, 1, 2]
zero, one, two = iterable
This will produce the same result as the above long version.
Back to your code,
x = y, y = x + y
the python interpreter will first work on the assignment (x = ...). To resolve it, it tries to unpack x + y into two values, but this is not possible of course.
To declare two variables on one line, you have to separate the assignments with a colon ;, not a comma. But when you do just that, x will have the same value as y at the beginning, and so y will then be the double of itself, which surely is not what you wanted. Maybe you should do it like this ?
a = []
x = 1
y = 2
for y in range (1, 400000):
if x % 2:
a.append(x)
x, y = y, x + y
# above is equivalent to
# temp = y
# y = x + y
# x = temp
print(sum(a))

Function call error in Python

I have a 1D array X with both +/- elements. I'm isolating their signs as follows:
idxN, idxP = X<0, X>=0
Now I want to create an array whose value depends on the sign of X. I was trying to compute this but it gives the captioned syntax error.
y(idxN) = [math.log(1+np.exp(x)) for x in X(idxN)]
y(idxP) = X(idxP)+[math.log(np.exp(-x)+1) for x in X(idxP)];
Is the LHS assignment the culprit?
Thanks.
[Edit] The full code is as follows:
y = np.zeros(X.shape)
idxN, idxP = X<0, X>=0
y(idxN) = [math.log(1+np.exp(x)) for x in X(idxN)]
y(idxP) = X(idxP)+[math.log(np.exp(-x)+1) for x in X(idxP)];
return y
The traceback is:
y(idxN) = [math.log(1+np.exp(x)) for x in X(idxN)]
File "<ipython-input-63-9a4488f04660>", line 1
y(idxN) = [math.log(1+np.exp(x)) for x in X(idxN)]
^
SyntaxError: can't assign to function call
In some programming languages like Matlab, indexes are references with parentheses. In Python, indexes are represented with square brackets.
If I have a list, mylist = [1,2,3,4], I reference elements like this:
> mylist[1]
2
Wen you say y(idxN), Python thinks you are trying to pass idxN as an argument a function named y.
I got it to work like this:
y = np.zeros(X.shape)
idxN, idxP = X<0, X>=0
yn,yp,xn,xp = y[idxN], y[idxP],X[idxN],X[idxP]
yn = [math.log(1+np.exp(x)) for x in xn]
yp = xp+[math.log(np.exp(-x)+1) for x in xp];
If there is a better way, please let me know. Thanks.

Unsupported Operand Types Python Power and Lists

One of the tutorial questions has us focus on creating a function which returns a simple expression. The expression is below:
EQ = cos*(pi/2)*x^2 + sin(pi/4)*x + x^3
Where x is an array of floats.
I have written the following code to answer this:
def getStats(x):
# Complete the function.
y = float(numpy.cos((numpy.pi/2)*x**2) + numpy.sin((numpy.pi/4)*x) + x**3)
return y
However I get an error saying unsupported operand types for pow or ** list or int.
I have tried to type cast this but it has not worked.
Apart from type casting I have tried breaking this down further but have been unsuccessful. Any ideas on what I can try next?
Thank you.
You should evaluate the expression for each value of the input list. You can do this using list comprehension.
def getStats(X):
# Complete the function.
y = [float(numpy.cos((numpy.pi/2)*x**2) + numpy.sin((numpy.pi/4)*x) + x**3) for x in X]
return y
The approach in the edit section is preferred (see below).
Edit: Converting the input list to numpy array and do the computation.
def getStats(x):
# Complete the function.
x = numpy.array(x)
y = numpy.cos((numpy.pi/2) * numpy.power(x, 2)) + numpy.sin((numpy.pi/4)*x) + numpy.power(x, 3)
return y # if you want to return a list, use list(y)

multidimensional Euler's method python

So I have coded a function for Euler's method. However, I want it to be able to use initial conditions with arbitrary dimensions. So for example, currently my functions works using this:
>>>Euler(f, x0, t0, h, N)
where x0 is a float. However I want it to be able to use this:
>>>Euler(f, [x0], t0, h, N)
where x0 is now a list of floats. (making it multidimensional)
f = function, x0 = initial conditions at time t0,
t0 = initial time, h = step size, N = number of steps.
I have tried using a for loop:
def Euler(f,x0,t0,h,N):
t = t0
y = x0
z = []
v = []
for i in y:
while t <= N:
xval = t
yval = [y]
t += h
y += h * f(t,y[i]) #i have also tried y+= h*f(t, i)
z.append(xval)
v.append(yval)
return z, v
The error I get is TypeError: list indices must be integers or slices, not float. Which I understand, meaning I have to index the y, like using y[0], y[1], etc...but when I do
y+= h* f(t, y[:])
It gives me an error regarding my other function in the file : f = >
TypeError: a float is required
line 22, in <module> vv = -x**3 - x + sin(t)
When I also try
y += h * f(t, y[0])
and I enter
>>>Euler(f, [0., 1.], 0., 1, 10)
line 15, in <module>
y += h * f(t,y[0])
builtins.TypeError: 'float' object is not iterable
I essentially want to return 2 lists, first list is the z, where it returns a list of the time values, and the second list v, where it returns a list of list of each of the results during each step. So far it has worked where I used a float but not a list. So what code am I missing?
Try this:
def Euler(f,x0,t0,h,N):
t = t0
z = []
v = []
for y in x0:
while t <= N:
xval = t
yval = [y]
t += h
y += h * f(t,y) #i have also tried y+= h*f(t, i)
z.append(xval)
v.append(yval)
return z, v
I don't know if this is the intended method seeing as y += h * f(t,y) is dead code and is not used anywhere else
I believe the error is due to not paying attention to the types of your variables. y was a list when you did y = x0.
Fast-forward to this line y += h * f(t,y[i]). In here you try to use the += operator on y and what this does is to append the contents of another iterable to y.
In the same statement, you try to index into y using i. To index into a list, you need to use an integer, but since i is an element of y (which is an array of floats), i cannot be used to index into the list, and this is why you can the error:
TypeError: list indices must be integers or slices, not float.
Also when you do this y+= h* f(t, y[:]), you get the error:
TypeError: a float is required
Because y[:] creates a new list with all the elements of y, thus you are still passing a list to your function.
Finally when you do this y += h * f(t, y[0]), you get the error:
builtins.TypeError: 'float' object is not iterable
Because as I mentioned before, y is a list and += on a list appends the contents of another iterable to the current list. The way it does this is to "iterate" over the second list and append the items in that second list to the first. Since the value h * f(t, y[0]) is not a list, and is not an iterable either, you get the error

'int' object has no attribute '__getitem__' Python

My code is method of Euller for second ODE. I already try to do a function to define f this way
{def inicial():
global f
f=matrix(M,N)}
But I had problem in the same line. I don't know how to recognize my function in that line.
N=101
x_min = -10.0
x_max = 10.0;
dx = (x_max - x_min)/(N-1)
dt = 0.25*dx*dx
t=0
t_max = 1000
Q=1
j=0
M=2
f = [N , M]
def f_xx(i,t):
return ((f[i+1][t]-2*f[i][t]+f[i-1][t])/(dx*dx))
def guess(x):
return ((pi*Q/x_max)*x +(pi*Q))
for i in range (N):
for j in range(j):
x = x_min + i*dx
f[i][j] = guess(x)
for j in range(t_max+1):
for i in range(N-1):
x = x_min + i*dx
f[i][j+1] = f[i][j]+(f_xx(i,j)-sin(f[i][j]))*dt <<<error
for i in range (N-1):
f[i][j] = f[i][j+1]
What does mean 'int' object has no attribute getitem? Could anyone help fix it?
f is a list containing two ints. f[i] refers to the the i'th int; so f[i][j], will try and get the j'th value of an int, which cannot possibly work, whatever the value of j.
It's not clear what you are trying to do with this call, though.
You are trying to use f as a list of lists (i.e. like a 2-dimensional array), but it's really just one list: [101, 2].
I'm rusty on ODEs but I think you were trying to create a 101x2 grid of 0s. If so try f = [[0.0]*M for x in range(N)].

Categories

Resources