Sudoadmin
Sudoadmin

Reputation: 35

Numpy error in Python

I am making a text based game and numpy comes back with the error

   File "maingame.py", line 60, in <module>
    if arr[character] == 20:
ValueError: The truth value of an array with more than one element is ambiguous. Use      a.any() or a.all()

if I use either of the two suggestions it says

  File "maingame.py", line 60, in <module>
    if arr[character] == arr.all(20):
  File "/usr/lib/python2.7/dist-packages/numpy/core/_methods.py", line 37, in _all
    keepdims=keepdims)
ValueError: 'axis' entry is out of bounds

my array is 56 x 43

I've already tried to expand the boundaries of the array

thanks

new error:

  File "maingame.py", line 70, in <module>
    caracterx = charaxterx - 1
NameError: name 'charaxterx' is not defined

my code:

characterx = 0
charactery = 0
character = [charactery,characterx]
if np.all(arr[character] == 20):
    print "You are is a plain. You can see far in the distance all around you"
if np.all(arr[character] == 20):
    print "You are in a city. There are tall buildings all around you. It appears to be abandoned"
if np.all(arr[character] == 20):
    print "You are in a sparse forest. It is loosely wooded."
if np.all(arr[character] == 20):
   print "You are in a dense forest. You can only see a couple meters in each direction"

Upvotes: 0

Views: 315

Answers (1)

NPE
NPE

Reputation: 500357

If you are trying to check whether all elements of arr[character] equal 20, write:

if numpy.all(arr[character] == 20):

The arr[character] == 20 returns a boolean array, and numpy.all() checks that all elements of that array are true.

Upvotes: 2

Related Questions