我有以下Python代码:
currentPlayers = query.getPlayers() for player in currentPlayers: return str(player['name'])+" "+str(player['score'])
我收到以下错误:
TypeError:列表索引必须是整数,而不是str
我一直在寻找接近我的错误,但不确定如何执行此操作,但从未收到该错误。是的,如何将其转换为整数而不是字符串?我想问题出在哪里str(player['score'])。
str(player['score'])
您是否期望player成为一个dict而不是一个list?
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