【发布时间】:2015-04-13 13:19:26
【问题描述】:
我正在用 python 设计一个井字游戏,计算用户通过三个级别所需的时间,然后将这个分数保存到一个名为“Player Times.txt”的文件中。我想对每个玩家从低 => 高的次数进行排名。
print('You beaten all three levels and reached the end of the game!')
.windll.user32.MessageBoxW(0, "Hooray you've won the game.","Victory", 1)
ctypes.windll.user32.MessageBoxW(0, str(sum1), "Your Score", 1)
#top_score=str(input('Would you like to see the leaderbooard?'))
MyfileWrite = open('Player Times.txt','a')
MyfileWrite.write(file_info + "'s time is " + str(sum1) + '.' + '\n')
MyfileWrite.close()
print('--------------------')
top_score=str(input('Would you like to see the leaderbooard?'))
print('--------------------')
if (top_score=='yes'):
MyfileWrite = open('Player Times.txt', 'r')
file_contents = MyfileWrite.read()
print(file_contents)
MyfileWrite.close()
所以这就是当你击败三个级别时会发生的事情。然后您可以查看排行榜,但它只会打印出所有已写入文件的时间。我该怎么做才能对这些时间进行排名?
【问题讨论】:
-
print(file_contents) 将一直打印出来。您必须以某种方式对时间进行排序并打印所需的时间。
-
我该怎么做?