出于这个问题的目的,我已将其缩短,但代码如下所示:
total1=0
total2=0
total3=0
score1=20
score2=30
score3=40
players = [{'user': 'Player1', 'total': total1, 'score': score1},
{'user': 'Player2', 'total': total2, 'score': score2},
{'user': 'Player3', 'total': total3, 'score': score3}]
for i in players:
if players[i]['score'] <= 30:
***code goes here***
我得到这个 TypeError: list indices must be integers, not dict
我怎么说“如果每个玩家得分的值 <= 30”?
如果我只写print players[0]['score']
我得到 20。如果我写print players[1]['score']
我得到 30,但为什么我不能把它放在一个 for 循环中并让“i”成为数字?
提前致谢!