Search code examples
pythonpython-3.xpysimpleguisteam-web-api

Function stops iterating over loaded json after the first element


I am trying to get every achievement someone has in Team Fortress 2 and I have got it to the point where it gets the achievements and did iterate over them but now does not. This is the function and where it is being ran

Data.py

def resolveAchievements(ac):
    _ac = json.loads(ac)
    for achievement in _ac["playerstats"]["achievements"]:
        print(achievement)
        return acName.append(achievement["name"]), acDesc.append(achievement["description"]), acUnlock.append(achievement["achieved"])

Main.py

for a in acName:
     addText(a)

And the addText function if its needed:

def addText(t):
        layout.append([
             psg.Text(
             text=t,
             font=('Arial Bold', 14),
             size=14,
             expand_x=True,
             justification='center'
             )
        ])

It is only printing the first achievement twice and is saying None [] after. Here is what is being printed to the terminal:

{'apiname': 'TF_PLAY_GAME_EVERYCLASS', 'achieved': 0, 'unlocktime': 0, 'name': 'Head of the Class', 'description': 'Play a complete round with every class.'}
{'apiname': 'TF_PLAY_GAME_EVERYCLASS', 'achieved': 0, 'unlocktime': 0, 'name': 'Head of the Class', 'description': 'Play a complete round with every class.'}
None []

I am not sure what is going on but any help is appreciated!


Solution

  • Your return statement is in the loop, so you immediately return from the function (terminating the loop) after the first iteration.

    I suspect you simply want to append values to three separate lists, then returning the three lists after the loop is complete.

    def resolveAchievements(ac):
        _ac = json.loads(ac)
        acName = []
        acDesc = []
        acUnlock = []
    
        for achievement in _ac["playerstats"]["achievements"]:
            acName.append(achievement["name"])
            acDesc.append(achievement["description"])
            acUnlock.append(achievement["achieved"])
    
        return acName, acDesc, acUnlock