django 给定的变量值错误

np8igboo  于 2023-01-31  发布在  Go
关注(0)|答案(3)|浏览(109)

所以我遇到了一些变量的问题,这些变量来自字典迭代:

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"

以下是此字典的循环:

[{"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}]

我遇到的问题是,第二个for循环的else部分中的所有变量都是"None"值,这不应该是这种情况......不过,Flex的循环工作得非常好。
我已经试过改变了:

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

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

cnwbcb6i1#

每次通过循环时,都要完全重置变量solo_rank_namesolo_tier等。
在最后一次循环迭代中,queueType为"RANKED_FLEX_SR",因此执行else分支,并将所有变量设置为"None",就像代码所说的那样。
你以为会发生什么?

zpf6vheq

zpf6vheq2#

将两个if语句更改为一个if elif else语句,它应该可以修复此问题,单独执行此操作将覆盖前一个语句,因为它与当前匹配项不匹配。

blmhpbnm

blmhpbnm3#

我能够修复这段代码如下:

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

我有一点误解,我只是先给变量赋值,然后把循环放在变量后面,这样如果条件满足,它就会覆盖变量的值。谢谢你的覆盖思想!

相关问题