Home > Mobile >  TypeError: string indices must be integers in the chess.com api
TypeError: string indices must be integers in the chess.com api

Time:01-06

SO i was watching this tutorial (https://www.youtube.com/watch?v=KYNbHGs-qG4)minute:14.50 to 15.14 by "tech with Tim" about the api and he put the username and ranks of the players but at me just give me the error "TypeError: string indices must be integers" but for him works

from chessdotcom import get_leaderboards
import pprint

printer = pprint.PrettyPrinter()


def print_leaderboards():
    data = get_leaderboards().json
    categories = data.keys()

    for category in categories:
        print('Category:', category)
        # idx = index
        for idx, entry in enumerate(data[category]):
            print(f'Rank: {idx   1} | Username: {entry["username"]} | Rating: {entry["score"]}')


print_leaderboards()

CodePudding user response:

Because there are no such keys inside "data[category]".

You need to add a subcategory from keys: ['daily', 'daily960', 'live_rapid', 'live_blitz', 'live_bullet', 'live_bughouse', 'live_blitz960', 'live_threecheck', 'live_crazyhouse', 'live_kingofthehill', 'tactics', 'rush', ' battle']

And line with FOR loop will be like:

for idx, entry in enumerate(data[category]['daily']):

And you will see:

Rank: 1 | Username: igorkovalenko | Rating: 2715
Rank: 2 | Username: RWHaines | Rating: 2604
Rank: 3 | Username: Zgorl | Rating: 2601
Rank: 4 | Username: francisbegbie | Rating: 2513
Rank: 5 | Username: Ryzeaf | Rating: 2509
Rank: 6 | Username: if_name_main_a | Rating: 2508
Rank: 7 | Username: JolinTsai | Rating: 2506
  • Related