importtimedefplay_game():""" play a game and return name, finalscore;return None to mean no more games, program finished.THIS function you'd better code yourself!-)"""defscoreToFile(name,finalScore):""" Add a name and score to the high-scores file. """currentTime=time.strftime("%c")fileOutput=finalScore,"-",name,currentTimeline=' '.join(str(x)forxinfileOutput)withopen("highscores.txt","a")asf:f.write(line+"\n")defminus_score(line):""" just for sorting purposes, not called directly. """return-int(line.split()[0])defsorted_by_score():""" return list of score lines sorted in descending order of score. """withopen("highscores.txt","r")asf:returnsorted(f,key=minus_score)defmain():whileTrue:game_result=play_game()ifgame_resultisNone:breakscoreToFile(*game_result)forlineinsorted_by_score:print(line.strip())
python 写txt 换行_写入文件python-换行问题(\ n)
最新推荐文章于 2024-03-06 16:37:41 发布