Python error when trying to access list by index - “List indices must be integers, not str”

前端 未结 4 403
星月不相逢
星月不相逢 2020-12-16 09:54

I have the following Python code :

currentPlayers = query.getPlayers()
    for player in currentPlayers:
        return str(player[\'name\'])+\" \"+str(playe         


        
4条回答
  •  离开以前
    2020-12-16 10:13

    Were you expecting player to be a dict rather than a list?

    >>> player=[1,2,3]
    >>> player["score"]
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: list indices must be integers, not str
    >>> player={'score':1, 'age': 2, "foo":3}
    >>> player['score']
    1
    

提交回复
热议问题