我在这段代码中遇到了很多问题,但我再次难倒如何做到这一点。
我想将分数和用户名添加到一个外部文件中,该文件保留在该文件中,以后可以在另一场比赛中作为前 5 名分数和获得它们的人访问。到目前为止,我已经得到了这个:
score = '11'
gametag = 'Griminal'
with open("scores.txt", "a+") as out_file:
print(out_file)
out_string = ""
out_string += str(score) + " points from: " + str(gametag)
out_string += "\n"
print(out_string)
out_file.append(out_string)
print(out_file)
但是,正如我注意到的那样,该文件没有作为列表打开,而是作为:
<_io.TextIOWrapper name='scores.txt' mode='a+' encoding='cp1252'>
当我运行 print(out_file) 时,它会被打印到外壳中
所以我无法将新分数附加到列表中并将其保存到文件中。有没有人有解决这些问题的方法?
要对其进行排序,我有以下代码:
f = sorted(scores, key=lambda x: x[1], reverse=True)
top5 = f[:5]
print(top5)
据我所知,这有效。
我收到的错误代码是:
Traceback (most recent call last):
File "C:/Users/gemma/OneDrive/Desktop/Gcse coursework.py", line 60, in
<module>
out_file.append(out_string)
AttributeError: '_io.TextIOWrapper' object has no attribute 'append'
UYOU
相关分类