这是我到目前为止的代码
def Save_Scores():
global score
score=str(score)
file=open("Class{}.txt",'a').format(group)
file.write("\n")
file.write ("Name: "+name+"\n")
file.write("Score: "+score+"/10\n")
file.close()
quiz()
但是在运行该函数时遇到此错误
line 42, in Save_Scores
file=open("Class{}.txt",'a').format(group)
AttributeError: '_io.TextIOWrapper' object has no attribute 'format'
答案 0 :(得分:3)
R:0,G:0,B:0.
是字符串对象上的一种方法,但您尝试在文件上调用它。将其应用于您要传递给str.format()
电话的字符串:
open()
您可以在此处申请的其他最佳做法:
不要将file = open("Class{}.txt".format(group), 'a')
用作全局,而是将其作为函数的参数,然后在调用函数时将其传入。
您还使用score
和name
作为全局变量,这些也应该是参数。
使用group
语句让Python为您关闭文件。
您也可以对写入文件的数据使用字符串格式,而不是使用连接。
通过这些更改,您的功能将如下所示:
with