我已经在python中编写了一个程序,该程序应该返回每个级别的得分最高的玩家,但它会一直返回" KeyError:20"对于第69行(P_L1 = players_TS [H_L1]),我无法解决如何解决这个问题。在我的代码中,我打开了一个名为" playerScores.txt"的文本文件。格式如下:
月亮,3,15
StarPlayer,2,18
艾莉,5,9
等
这是我的代码:
print("The highest scorers for each leve are:\n")
PS3=open("PlayerScores.txt","r").read().split("\n")
level1_scores=[]
players_TS={}
level2_scores=[]
players_T2={}
level3_scores=[]
players_T3={}
level4_scores=[]
players_T4={}
level5_scores=[]
players_T5={}
for line in PS3:
lines=line.split(",")
player=lines[0]
level=lines[1]
score=lines[2]
erros=open("scoreboard_errorlog.text","w")
if int(score)>20:
errors.write(score)
if int(level)>5:
errors.write(level)
if int(level)==1:
level1_scores.append(int(score))
players_TS.setdefault(score,[]).append(player)
if int(level)==2:
level2_scores.append(int(score))
players_T2.setdefault(score,[]).append(player)
if int(level)==3:
level3_scores.append(int(score))
players_T3.setdefault(score,[]).append(player)
if int(level)==4:
level4_scores.append(int(score))
players_T4.setdefault(score,[]).append(player)
if int(level)==5:
level5_scores.append(int(score))
players_T5.setdefault(score,[]).append(player)
H_L1=max(level1_scores)
P_L1=players_TS[H_L1]
H_L2=max(level2_scores)
P_L2=players_T2[H_L2]
H_L3=max(level3_scores)
P_L3=players_T3[H_L3]
H_L4=max(level4_scores)
P_L4=players_T4[H_L4]
H_L5=max(level5_scores)
P_L5=players_T5[H_L5]
print("Level\tPlayer name\tScore\nLevel 1\t",P_L1,"\t",H_L1,"\nLevel 2\t",P_L2,"\t",H_L2,"\nLevel 3\t",P_L3,"\t",H_L3,"\nLevel 4\t",P_L4,"\t",H_L4,"\nLevel 5\t",P_L5,"\t",H_L5)
非常感谢帮助。
答案 0 :(得分:2)
问题出现在这里:
if int(level)==1:
level1_scores.append(int(score))
players_TS.setdefault(score,[]).append(player)
假设score == "20"
。您正在将整数 20
添加到level1_scores
,但使用字符串 "20"
作为players_TS
中的密钥。然后,当你到达这些行
H_L1=max(level1_scores)
P_L1=players_TS[H_L1]
H_L1
是整数值20
,但players_TS
中的所有键都是"20"
之类的字符串。自20 != "20"
起,KeyError
结果。
可能,您希望score
(和level
,就此而言)是一个整数,所以在解析之后立即转换一次,然后按原样使用score
在循环的其余部分。
for line in PS3:
lines = line.split(",")
player = lines[0]
level = int(lines[1])
score = int(lines[2])
with open("scoreboard_errorlog.text","w") as errors:
if score > 20:
errors.write(score)
if level > 5:
errors.write(level)
if level == 1:
level1_scores.append(score)
players_TS.setdefault(score,[]).append(player)
elif level == 2:
level2_scores.append(score)
players_T2.setdefault(score,[]).append(player)
elif level == 3:
level3_scores.append(score)
players_T3.setdefault(score,[]).append(player)
elif level == 4:
level4_scores.append(score)
players_T4.setdefault(score,[]).append(player)
elif level == 5:
level5_scores.append(score)
players_T5.setdefault(score,[]).append(player)
H_L1 = max(level1_scores)
P_L1 = players_TS[H_L1]
H_L2 = max(level2_scores)
P_L2 = players_T2[H_L2]
H_L3 = max(level3_scores)
P_L3 = players_T3[H_L3]
H_L4 = max(level4_scores)
P_L4 = players_T4[H_L4]
H_L5 = max(level5_scores)
P_L5 = players_T5[H_L5]
但是,您还应该使用dict
来存储大量此类数据,而不是单独命名的变量。
level_scores = {level: [] for level in range(1,6)}
players = {}
for line in PS3:
lines = line.split(",")
player = lines[0]
level = int(lines[1])
score = int(lines[2])
# Use append mode, or you overwrite the file each time through the loop
with open("scoreboard_errorlog.text", "a") as errors:
if score > 20:
errors.write(score)
if level > 5:
errors.write(level)
continue
level_scores[level].append(score)
players[level].setdefault(score,[]).append(player)
H = {}
P = {}
for level in range(1,6):
H[level] = max(level_scores[level])
P[level] = players[level][H[level]]