Home > Back-end >  Wrong value for variable is being given
Wrong value for variable is being given

Time:01-29

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":
                 ...

CodePudding user response:

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?

CodePudding user response:

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.

  • Related