我已经积累了一份名单,其中包括每个人的用户名和其所获得分数。
接着我使用了以下代码将这些分数按降序排列。
winnerScore.sort()
winnerScore.reverse()
以下是打印列表“winnerScore”时的结果。
['j 78', 'j 36', 'i 90', 'i 58']
该函数根据用户名而不是实际代码进行了排序。
负责对列表进行排序的函数如下:
global winnerScore
with open("diceRoll.txt","r") as x_file:
contents = x_file.readlines()
oneScore = contents[count-1]
oneScore = oneScore.split(" ")
print(oneScore)
n = oneScore[-2][-1] + " " + oneScore[-1]
winnerScore.append(n)
if len(oneScore) != 0:
winnerScore.sort()
winnerScore.reverse()
我已经从文本文件中读取了分数和用户名。
我该如何更改代码,以便根据用户名的实际分数对'winnerScore'列表进行排序?
['z 82', 'h 68', 'h 52', 'z 52']
。希望你没有忘记再次分配变量;)检查我的更新答案。 - Óscar López