我得到以下错误:Stats PlayerList=competitionList[j]KeyError:2 我在competitionList中保存了许多列表,这是一本字典。我想遍历字典中列表中的所有对象。你知道吗
def Stats(competitionList, playerList):
no_of_competitions = int(len(competitionList))
x = (len(playerList))
for i in range(no_of_competitions):
for j in range (int(x)):
#My error occurs here
PlayerList=competitionList[j]
for player in PlayerList:
print("player: ", player.name)
print ("list :", player.name, player.victories)
使用
j
访问competitionList
。但是j
指x
,x
是playerList
的长度。playerList
和competitionList
在长度上可以延迟,所以这就是为什么会出现这种类型的错误。你知道吗错误只是在这行中指出:
字典
competitionList
不包含range(x)
中的一个键,在本例中是键2
。确保:competitionList
对于第二种选择,这应该简化事情并使其工作:
相关问题 更多 >
编程相关推荐