Reputation: 587
I have some array with integers, and for loop. I am trying to test if some specific elements in array is bigger or smaller that some integer. This code explain it better:
array = [1,2,3,4,5]
for i in range(5):
if array[i] >= 3:
print(sometext)
else:
print(othertext)
But i got an ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
SOLUTION: I did indent it properly. This above is just simplification(some stupid example) of my code. I found where the error is. It is because I initialized array with numpy as
a = numpy.empty(5)
and not like this:
a = [0 for i in range(5)]
Thank you everybody for your help
Upvotes: 5
Views: 62448
Reputation: 1
I gave this a go
array = [1,2,3,4,5]
x1 = (array[0])
x2= (array[1])
x3= (array[2])
x4= (array[3])
x5= (array[4])
if x1 <= x2:
print("very good")
else:
print("also good")
how's that?
Upvotes: 0
Reputation: 123
This isn't really the most pythoninc way of doing what you're describing.
array = [1,2,3,4,5]
for element in array:
if element >= 3:
print("Yes")
else:
print("No")
Reference: https://wiki.python.org/moin/ForLoop
Upvotes: 0
Reputation: 617
The Error that you are getting is basically due to INDENTATION . Python strictly follows indentation , meaning that it will only execute codes written in that specific Block . Refer Python Indentation Rule for more details. Thank You. Happy Coding Ahead.
Upvotes: 0
Reputation: 61
It worked for me with proper intendations:
>>> array = [1,2,3,4,5]
>>> for i in range(5):
... if array[i] >= 3:
... print("Yes")
... else:
... print("No")
...
Upvotes: 2
Reputation: 1942
You should iterate over the array itself:
array = [1, 2, 3, 4, 5]
for item in array:
if item >= 3:
print("yes")
else:
print("no")
Upvotes: 4