Python中的numpy错误 [英] Numpy error in Python

查看:161
本文介绍了Python中的numpy错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作基于文本的游戏,并且numpy出现错误

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

我的数组是56 x 43

my array is 56 x 43

我已经尝试扩展数组的边界

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

谢谢

新错误:

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

我的代码:

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"

推荐答案

如果要检查arr[character]的所有元素是否都等于20,请输入:

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

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

arr[character] == 20返回一个布尔数组,并且numpy.all()检查该数组的所有元素都是正确的.

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

这篇关于Python中的numpy错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆