我正在尝试制作一个程序,它接受一个值,将其写入一个文本文件,然后重复该操作多达5次。问题是我似乎不知道如何使它把新的输入写到新行上。相反,它只是覆盖旧的输入。如何使每个输入都进入一个新行而不被覆盖?你知道吗
我的代码:
def main():
outfile = open('scorefiles.txt', 'w') #Makes the .txt fiel.
grade = input("Grade: ") #Gets input.
outfile.write(grade + "\n") #Writes input to txt file.
outfile.close() #Closes file.
for _ in range(5): #Repeats main function 5 times.
main()
您需要打开一次文件并多次写入,或者只需使用附加模式:
相关问题 更多 >
编程相关推荐