0

the part of my code that has a problem is:

history = np.array([[0, 0, 1, 1],[1, 0, 0, 1]])
opponentsActions = history[1]
if opponentsActions == [0, 0, 0, 0]:
    print("nice")

and the error I get is: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

  • Search with the error message - you will find plenty to read which will give you a feel for what the issue is. You probably want `if (opponentsActions == [0, 0, 0, 0]).all():` – wwii May 20 '21 at 19:02
  • [https://stackoverflow.com/a/22175728/2823755](https://stackoverflow.com/a/22175728/2823755) – wwii May 20 '21 at 19:03

2 Answers2

0

When you execute your check, you receive array representing comparision

>>> opponentsActions == [0, 0, 0, 0]
array([False,  True,  True, False])

You are prompted to use any() or all() and that's quite a helpful hint. All(something) means all elements of something you want to check has to be true. So in your case:

>>> np.all(opponentsActions == [0, 0, 0, 0])
False
SebaLenny
  • 46
  • 2
  • Thanks for explaining but because it is an if statement the "answer" wwii gave is more correct. Still thanks – yoav cohen May 20 '21 at 19:15
0

You are comparing a numpy array with Python list, that won't work, a quick workaround can be converting the numpy array to python list using tolist() where you are comparing, it will work that way, have a look:

history = np.array([[0, 0, 1, 1],[0, 0, 0, 0]])
opponentsActions = history[1]
print(opponentsActions)
if opponentsActions.tolist() == [0, 0, 0, 0]:
     print("nice")

Or you can use np.all as suggested by SebaLenny.