大家好,请帮助我,这是一个功能,排序的球员,他们的高分。但这不是分类
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
问题是它将分数排序为字符串(例如,“123”在“32”之前)
提示:在排序之前或在键函数中,需要将它们转换为整数
相关问题 更多 >
编程相关推荐