初学者 Python 列表错误:"List indices must be integers, not str" ?


attributes = [["strength", 0], ["health", 0], ["wisdom", 0], ["dexterity", 0]]
attrib = raw_input("Which attribute would you like to add these points to? ")
int(attrib)
print attributes[attrib][1]

输出:

List indices must be integers, not str

我希望属性是用户输入的数字。我不知道为什么我会收到此错误,因为在我看来,我已经将字符串转换为第 3 行的整数。

猜我不允许以这种方式访问嵌套列表?失去。。。

int(attrib)不会

attrib的内容"修改"为整数;它返回整数。你想要的是这个,相反:

attrib = int(attrib)

相关内容

最新更新