0

I made a maths quiz where the program asks and stores the user's name, the question, the solution and the user's score into a .csv file. My problem is that whenever the program is run the newly inputted data overwrites the previous data set, which I don't want so instead I want the new data to be appended below the old data set. How can I do this?

Here is my code so far:

import csv
import random

Name = input("Enter your name: ")
score = 0

a = random.randint(1,100)
b = random.randint(1,100)
Q1 = int(input(str(a) + " + " + str(b) + " = "))
A1 = a + b

x = random.randint(1,100)
y = random.randint(1,100)
Q2 = int(input(str(x) + " - " + str(y) + " = "))
A2 = x - y

if Q1 == A1:
    score1 = score + 1
else:
    score1 = score


if Q2 == A2:
    score2 = score + 1

else:
    score2 = score

with open("Quiz.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerow([Name, "Question", "Solution", "Mark"])

    writer.writerow([1, str(a) + " + " + str(b) , A1, score1])
    writer.writerow([2, str(x) + " - " + str(y) , A2, score2])
wasd237
  • 5
  • 2

1 Answers1

0

You need to open the file in append mode, like this:

with open("Quiz.csv", "a", newline="") as file:
JakeBoggs
  • 274
  • 4
  • 17