Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)

I have the following Python code :

currentPlayers = query.getPlayers() 

for player in currentPlayers: 

return str(player['name'])+" "+str(player['score'])

And I'm getting the following error:

TypeError: list indices must be integers, not str

I've been looking for an error close to mine, but not sure how to do it, never got that error. So yeah, how can I transform it to integers instead of string? I guess the problem comes from str(player['score']).

1 Answer

0 votes
by (106k points)

To get rid of this error you can use the below-mentioned code:-

>>> 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'] 


Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.4k questions

32.5k answers


108k users

Browse Categories