在多个函数调用中添加到文件而不是覆盖数据

2024-09-30 01:35:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个程序,它接受一个值,将其写入一个文本文件,然后重复该操作多达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()

Tags: the代码程序txtinputmaindefopen

热门问题