我创建了一个游戏,允许两名玩家掷两个骰子,持续5回合。恭喜获奖者,我想将获奖者的姓名和分数保存到一个外部文件(.txt或.csv)中。问题是我不知道如何执行此操作。我想将他们的姓名和分数按总分的顺序存储到外部文件中(并在其上放置其位置,例如第一,第二,第三等)。最后,我希望(在该游戏中保存了比赛中获胜者的得分之后)展示该排行榜中的前5名。我将在下面保留相关代码:
if number4 > number5:
print(player_one.upper() + " HAS WON THE SUDDEN DEATH ROUND AND ACQUIRED A TOTAL OF " + str(total_player_one_score) + " POINTS!" + "\n")
winning_player = player_one
winning_score = total_player_one_score
sudden_death = False
elif number5 < number4:
print(player_two.upper() + " HAS WON THE SUDDEN DEATH ROUND AND ACQUIRED A TOTAL OF " + str(total_player_two_score) + " POINTS!" + "\n")
winning_player = player_two
winning_score = total_player_two_score
sudden_death = False
print("CONGRATULATIONS TO THE WINNER! Your name and score will now be saved into our database.")
with open("leaderboard.csv", "a") as leaderboard: