python - 带有 Numpy 数组的 Python 中的 IndexError
问题描述
我有以下代码不断给我一个IndexError
,我根本不明白为什么:
mainMenu = np.array(['Load new data',
'Check for data errors',
'Generate plots',
'Display list of grades',
'Quit'])
if choice == 2 or choice == 3 or choice == 4:
print("Please load data before you {:s}".format(mainMenu[choice-1]))
continue
最后一部分是给出错误的原因:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
谁能看到这里有什么问题?
解决方案
嗨,当您定义choice
变量时,它似乎不是整数。例如,它被声明为choice = 4.0
而不是choice=4
我建议进行以下更改
if choice == 2 or choice == 3 or choice == 4:
print("Please load data before you {:s}".format(mainMenu[int(choice)-1]))
continue
推荐阅读
- vue.js - 从 vue.js json 父组件继承子组件
- javascript - React Native - 使用 Expo Audio 在录制声音的同时播放声音
- javascript - vue-cli 使用官方 pwa 插件创建的网站在离线模式下不起作用
- php - laravel + mssql 应用推荐的服务器规范
- python - 为什么我在训练数据时会收到此错误?
- node.js - 中间件不起作用,尽管给出了下一个语句
- python - 如何使用 SymPy 求解两个变量方程以使其依赖于一个变量?
- python - 在 pandas 中插入数据
- python - 获取单个模型的多个查询集列表
- php - 使用来自 FormRequest Laravel 的会话重定向