我已经为我一直在做的一个更大的测验做了一个领导委员会。我编写了一些代码,可以打开文本文件,并在运行时"\n"
将文本文件中的内容打印到不同的行上。但是,当运行时,它不仅像应该显示的那样显示名称和分数,还显示应该隐藏的新行\n
。我该怎么解决这个问题?
这是我遇到的问题:
if score == 3:
print("You have reached the maximum amount of points and have reached the top of the current leaderboard, congrats.")
leaderboard = open ("leaderboard.txt","r")
write_in_file(leaderboard, score, username)
topscores = leaderboard.readlines()
print(topscores)
任何帮助都将不胜感激,因为这项评估有一个很快就要到的时限。你知道吗
您可以指定end是
print()
语句本身中的换行符正如MohitC所建议的,你可以使用列表理解。在你发布的代码中,你打开了文件,但没有关闭它。我建议您关闭它,或者更好的做法是,将来使用以下语法:
当您超出范围时,文件将自动关闭。你知道吗
因此,使用这两个建议,您可以使用以下代码:
相关问题 更多 >
编程相关推荐