【发布时间】:2018-12-27 12:16:16
【问题描述】:
我正在制作一个高分模块,目前只显示获得的前 5 名分数,我希望能够像我目前一样显示这些分数,但也显示与该分数相关联的名称,如何我会这样做吗?
这是我目前的代码:
def highscore():
file_highscore = open('scores_test2.txt' , 'r')
scores_and_names = []
scores = []
scores_2 = []
names = []
for line in file_highscore.readlines():
score_info = line.split()
scores_and_names.append(line)
scores_2.append(scores_and_names[line][])
scores.append(score_info[1])
names.append(score_info[0])
scores.sort(key = int)
scores.sort(reverse = True)
print('The First 5 Highscores are:')
for item in scores[:5]:
print(item)
任何帮助将不胜感激,因为我需要将此代码用于学校作业。
【问题讨论】:
-
到目前为止你尝试过什么? “scores_test2.txt”中的一行是什么样的?
-
将您的高分存储为字典 - 在欺骗here 中查看我的答案 - 字典更适合存储键:值项。还有其他存储它们的方法 - 请参阅其他答案(Json、Pickle、...)
标签: python python-3.x