Wrong value for variable is being given

Question:

so I have a problem with some variables, which are coming from an dictionary iteration:

for rank in ranked_stats:
    if rank['queueType'] == "RANKED_FLEX_SR":
        flex_rank_name = "Ranked Flex"
        flex_tier = rank["tier"]
        flex_rank = rank['rank']
        totalrank_flex = flex_tier + " " + flex_rank
        winrate_flex = rank['wins']/(rank['wins']+rank['losses'])
        winrate_flex *= 100
        winrate_flex = "{:.2f}%".format(winrate_flex)
        wins_flex = rank['wins']
        losses_flex = rank['losses']

    else:
        flex_rank_name = "None"
        flex_tier = "None"
        flex_rank = "None"
        totalrank_flex = "None"
        winrate_flex = "None"
        wins_flex= "None"
        losses_flex = "None"
            
        
for ranksolo in ranked_stats:  
    if ranksolo['queueType'] == "RANKED_SOLO_5x5":
        solo_rank_name = "Ranked Solo/Duo"
        solo_tier = ranksolo['tier']
        solo_rank = ranksolo['rank']
        totalrank_solo = solo_tier + " " + solo_rank
        winrate_solo = ranksolo['wins']/(ranksolo['wins']+ranksolo['losses'])
        winrate_solo *= 100
        winrate_solo = "{:.2f}%".format(winrate_solo)
        wins_solo = ranksolo['wins']
        losses_solo = ranksolo['losses']
     else:
        solo_rank_name = "None"
        solo_tier = "None"
        solo_rank = "None"
        totalrank_solo = "None"
        winrate_solo = "None"
        wins_solo= "None"
        losses_solo = "None"

These are the loops for this dictionary:

[{"leagueId": "0b36ed94-33bc-43e3-aa39-3bff2350f76e", "queueType": "RANKED_SOLO_5x5", "tier": "BRONZE", "rank": "II", "summonerId": "___hidden__", "summonerName": "lantern is lava", "leaguePoints": 57, "wins": 8, "losses": 5, "veteran": false, "inactive": false, "freshBlood": false, "hotStreak": false}, {"leagueId": "52002724-73b2-49bc-ad7b-ae58c64f2623", "queueType": "RANKED_FLEX_SR", "tier": "BRONZE", "rank": "II", "summonerId": "__hidden__", "summonerName": "lantern is lava", "leaguePoints": 1, "wins": 5, "losses": 5, "veteran": false, "inactive": false, "freshBlood": false, "hotStreak": false}]

The problem I have is that all variables in the else section of the second for loop are values with "None" which should not be the case….The loop for Flex is working perfectly fine though.

I already tried to change:

for ranksolo in ranked_stats:  
    if ranksolo['queueType'] == "RANKED_SOLO_5x5":

to

for rank_solo in ranked_stats:  
    if rank_solo['queueType'] == "RANKED_SOLO_5x5":
                 ...

Asked By: Exo

||

Answers:

You’re completely resetting the variables solo_rank_name, solo_tier etc. each time through the loop.

On the final loop iteration the queueType is "RANKED_FLEX_SR", so the else branch is executed and all the variables are set to "None", just as the code says to do.

What did you expect to happen?

Answered By: John Gordon

Change the two if statements into one if elif else statement and it should fix the issue, doing it seperately is overwriting the previous one because it doesnt match the current match.

Answered By: HWW

I was able to fix this segment of code as follows:

solo_rank_name = "None"
solo_tier = "None"
solo_rank = "None"
totalrank_solo = "None"
winrate_solo = "None"
wins_solo= "None"
losses_solo = "None"
flex_rank_name = "None"
flex_tier = "None"
flex_rank = "None"
totalrank_flex = "None"
winrate_flex = "None"
wins_flex= "None"
losses_flex = "None"
        
        
    
    
    for rank in ranked_stats:
        if rank['queueType'] == "RANKED_FLEX_SR":
            flex_rank_name = "Ranked Flex"
            flex_tier = rank["tier"]
            flex_rank = rank['rank']
            totalrank_flex = flex_tier + " " + flex_rank
            winrate_flex = rank['wins']/(rank['wins']+rank['losses'])
            winrate_flex *= 100
            winrate_flex = "{:.2f}%".format(winrate_flex)
            wins_flex = rank['wins']
            losses_flex = rank['losses']

        elif rank['queueType'] == "RANKED_SOLO_5x5":
            solo_rank_name = "Ranked Solo/Duo"
            solo_tier = rank['tier']
            solo_rank = rank['rank']
            totalrank_solo = solo_tier + " " + solo_rank
            winrate_solo = rank['wins']/(rank['wins']+rank['losses'])
            winrate_solo *= 100
            winrate_solo = "{:.2f}%".format(winrate_solo)
            wins_solo = rank['wins']
            losses_solo = rank['losses']

I realized the best approach was to initialized all keys in the dictionary to None. Subsequently I looped through the dictionary keys and, when ever the rank[‘queueType’] condition was met, I overwrote the variable values with those provided by the relevant dictionary values.
Thanks to John Gordon for his explanation and pointing me in the right direction.

Answered By: Exo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.