2 回答

TA贡献1806条经验 获得超8个赞
您可以以多种格式存储分数,我将使用json,因为它是可读且内置的:
score_file = r"/pat/to/score/file.json"
# at the beginning of your script: load the current scores
try:
with open(score_file, 'r') as f:
scores = json.load(f)
except FileNotFoundError:
scores = {}
# (...)
# during your script: update the scores with the player's new
scores[player_name] = new_score
# (...)
# at the end of your script: store the new scores
with open(score_file, 'w+') as f:
json.dump(scores, f)
打印你的分数排序:
scores_sorted = sorted([(p, s) for p, s in scores.items()], reverse=True, key=lambda x: x[1])
for player, score in scores_sorted:
print(player, score)
添加回答
举报