你好有人请帮助我,这是一个以高分对球员进行分类的功能。但它并没有排序。
def sortByScore(inputPlayerScore):
return inputPlayerScore[1]
def highscore():
try:
scores = open("scores.txt", "r")
highScores = list() # place all your processed lines in here
for line in scores.readlines():
lineParts = line.split(": ")
if len(lineParts) > 1:
lineParts[-1] = lineParts[-1].replace("\n", "")
highScores.append(lineParts) # sorting uses lists
print(sorted(highScores, key=sortByScore, reverse = True)) # get this out of for loop
except Exception:
pass
输入文件是:
jkh: 32
daasd: 6
dasdf: 9
wfsdwf: 125
dada: 5
jkh: 62
答案 0 :(得分:0)
问题在于它将分数排序为字符串(例如,“123”出现在“32”之前)。
提示:您需要在排序前或在关键功能中将它们转换为整数。