My program is a maths quiz. The user completes 10 questions and then gets a score. That score then needs to be appended to the text file.
if classs == "1":
text_file = open("Class1.txt", "r")
data = text_file.read()
text_file.close()
if name in data:
searchName(name, score)
else:
text_file = open("newFileName.txt","a")
text_file.write(str(name) +","+ str(score)+ "\n")
text_file.close()
questions = 0
score = 0
break
This block of code appends that users score and name to the text file.
However I need to make it so if the user has already taken the quiz, the score is appended to that name instead of creating a new value. The score should be appended until there are three scores to one name, then if the quiz is taken again, it'll remove the first score and append the newest.
def searchName(name, score):
with open("Class1.txt", 'r') as f:
data = f.read().split('\n')
print(data)
with open("newFileName.txt", 'w') as newFile:
for line in data:
if name in line:
line+=(',') + str(score) + (',')
newFile.write(line + '\n')
But it only saves 1 score and removes it once the quiz is taken again.
Dion,0
Jarrod,1
Jake Bing,6
Charlie,2,5
Reverts to
Dion,0
Jarrod,1
Jake Bing,6,5
Charlie,2,
If another user uses the quiz (Jake Bing)