Q:

Adding to a file instead of overwriting data on multiple function calls

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()

A:

You need to open the file once and write many times, or just use the append mode:
outfile = open('scorefiles.txt', 'a')

