我有以下 Python 代码:
currentPlayers = query.getPlayers()
for player in currentPlayers:
return str(player['name'])+" "+str(player['score'])
我收到以下错误:
TypeError: list indices must be integers, not str
我一直在寻找与我接近的错误,但不知道该怎么做,从未遇到过该错误。所以是的,我怎样才能将它转换为整数而不是字符串?我猜问题出在 str(player['score'])
.
最佳答案
您是否希望 player
成为 dict
而不是 list
?
>>> player=[1,2,3]
>>> player["score"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not str
>>> player={'score':1, 'age': 2, "foo":3}
>>> player['score']
1
关于尝试按索引访问列表时出现 Python 错误 - "List indices must be integers, not str",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14198821/