-1

I am writing a programme that is a quiz that, in a nutshell, asks the users questions and stores their name and score on a text file in this format:

 Name: Humzah Score: 10
 Name: Mohammed Score: 15

As you can see it stores their information successfully. However, I want it to be that if a user redid the quiz with the same name, it would check to see if the name is on the text file and will add the score to the line e.g

 Name: Humzah Score:10 Score: 5

This is the code I have so far:

file=open("DataforScores.txt", "a")
     def checkUserExists(user):

I have searched for similar questions ,however, the answers are too cryptic and it doesn't answer my second question. How do I then add to that line?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • The code you posted is nonsense. Which Python textbook or tutorial are you using? – PM 2Ring Mar 26 '16 at 12:12
  • Welcome to stackoverflow. Your task is a common one and there are plenty of helpful resources, but to benefit from them you need to learn how to program a little. Then you'll be able to understand, and use, the answers to questions like [this one](http://stackoverflow.com/questions/4719438/editing-specific-line-in-text-file-in-python). – alexis Mar 26 '16 at 12:18

1 Answers1

1

Please take a look here: Editing specific line in text file in python

You should read the whole data from file, than edit needed line in memore and save it again.

Community
  • 1
  • 1
Aleks Lee
  • 136
  • 1
  • 1
  • 10
  • Ive re this but how do I know what line the Usersname is on? As it will always change I need a variable that will search for the name and the line it is on so I can append to it – Humzah MOHAMMED Mar 26 '16 at 11:52
  • You could read file line by line searching for the name in every string. Once it found you add new score to end of string, end then save new strings to the same files. Beware that strings (except last) end with '\n', so you should insert new score before this symbol. – Aleks Lee Mar 26 '16 at 12:18