我有以下代码一直给我一个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
任何人都可以看到问题在这里吗?
答案 0 :(得分:1)
您好,在定义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