0

I am trying to make a program that takes a value, writes it to a text file, and then repeats the action up to 5 times. The problem is I can't seem to figure out how to make it so that it writes the new input onto a new line. Instead it just overwrites the old input. How can I make it so that each input goes onto a new line without it being overwritten?

My code:

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()
sshashank124
  • 29,826
  • 8
  • 62
  • 75
KernelPanic
  • 201
  • 3
  • 9

1 Answers1

2

You need to open the file once and write many times, or just use the append mode:

outfile = open('scorefiles.txt', 'a')
Malik Brahimi
  • 15,933
  • 5
  • 33
  • 65