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

2
  • 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(): Commented May 20, 2021 at 19:02
  • https://stackoverflow.com/a/22175728/2823755 Commented May 20, 2021 at 19:03

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for explaining but because it is an if statement the "answer" wwii gave is more correct. Still thanks
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.

2 Comments

This could be a workaround but a simpler and shorter alternative would be like wwii wrote: "if (opponentsActions == [0, 0, 0, 0]).all():"
definitely, it will be!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.