【发布时间】:2013-01-14 17:54:01
【问题描述】:
出于这个问题的目的,我已将其缩短,但代码如下所示:
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***
我得到这个类型错误:列表索引必须是整数,而不是字典
“如果每个玩家得分的值
如果我只写print players[0]['score'],我会得到20。如果我写print players[1]['score'],我会得到30,但为什么我不能把它放在for循环中并让“i”成为数字?
提前致谢!
【问题讨论】:
-
永远记住这一点:在 Python 中“for”实际上是“foreach”。
标签: python if-statement dictionary for-loop key