title: Finding and adding to a record in a File
body: <p>I am fairly new to python and I need to make a program to ask 10 questions, save the score into a file and allow someone to read the scores in from the file. </p>
  
  <p><strong>My problem:</strong> I need to check if the person who has done the quiz already has a record in the file, and if so, I need to add their score to the end of their record. </p>
  
  <p>The records currently look like this: </p>
  
  <blockquote>
    <p>name,score,score,score,score,</p>
  </blockquote>
  
  <p>etc so they can be split using commas. </p>
  
  <p>I am also looking for the simplest answer, not the most efficient. 
  Also, if you could comment the code, it would make it much easier. Here is my code so far: </p>
  
  <pre><code>from random import randint
  import time
  import csv
  looping = 0
  correctAnswers = 0
  userNameValid = False
  classNoValid = False
  loop = 0
  
  while loop == 0:
      print("-------Maths Quiz-------")
      print("1. Play A Game")
      print("2. See Scores from previous games")
      print("3. Exit")
      playOrScores = input("Which option would you like to do? ")
      #^ Menu to find out which operation user would like to do. ^
  
  
  
  if playOrScores == "1":
  
      while userNameValid == False:
          userName = input("What is your name? ")
          if userName.isnumeric() == True:
              print("That username is invalid")
              userNameValid = False
          elif userName.isspace() == True:
              print("That username is invalid")
              userNameValid = False
          elif userName == "":
              print("Please input a username")
              userNameValid = False
          else:
              userNameValid = True
              #^Menu to validate users name input^
      while classNoValid == False:
          print("Which class are you in? ")
          print("1. Class 1a")
          print("2. Class 1b")
          classNo = input("3. Class 1c ")
          if classNo.isnumeric() == False:
              print("That input is invalid")
              classNoValid = False
          elif classNo.isspace() == True:
              print("That input is invalid")
              classNoValid = False
          elif classNo == "":
              print("Please input a class")
              classNoValid = False
          else:
              classNoValid = True
      #^menu to see which class the student is in and validate input ^
  
      while looping &lt; 10:                     #Allow the program to loop
          randomNumber1 = randint(1,10)
          randomNumber2 = randint(1,10)
          signNumber = randint(1,3)
          if signNumber == 1:
              correctAnswer = randomNumber1 + randomNumber2
              userAnswer = input("What is " + str(randomNumber1) + " + " + str(randomNumber2) + "?")
              if int(userAnswer) == correctAnswer:
                  print("Correct!")
                  correctAnswers += 1
              else:
                  print("Oh, sorry, the correct answer was " + str(correctAnswer))
          elif signNumber == 2:
              correctAnswer = randomNumber1 - randomNumber2
              userAnswer = input("What is " + str(randomNumber1) + " - " + str(randomNumber2) + "?")
              if int(userAnswer) == correctAnswer:
                  print("Correct!")
                  correctAnswers += 1
              else:
                  print("Oh, sorry, the correct answer was " + str(correctAnswer))
          elif signNumber == 3:
              correctAnswer = randomNumber1 * randomNumber2
              userAnswer = input("What is " + str(randomNumber1) + " x " + str(randomNumber2) + "?") 
              if int(userAnswer) == correctAnswer:
                  print("Correct!")
                  correctAnswers += 1
              else:
                  print("Oh, sorry, the correct answer was " + str(correctAnswer))
          looping += 1
      print("Well done " + userName + " Your final score was " + str(correctAnswers) + "!")
      print("")
      print("")
  
      if classNo == "1":
          class1 = open("Class1Scores.txt", "a+")
          #Opens a file in the name "Class1Scores"
          newRecord = userName+ "," + str(correctAnswers) + "," + "\n"
          #Creates a string containing the user's name and score, seperated by commas and with a new line at the end.
          class1.write(newRecord)
          #Writes this string to the file
          class1.close()
          #Closes the file
  
      elif classNo == "2":
          class2 = open("Class2Scores.txt", "a+")
          newRecord = userName+ "," + str(correctAnswers) + "," + "\n"
          class2.write(newRecord)
          class2.close()
  
      elif classNo == "3":
  
          class3 = open("Class3Scores.txt", "a+")
          newRecord = userName+ "," + str(correctAnswers) + "," + "\n"
          class3.write(newRecord)
          class3.close()
  
  
  elif playOrScores == "2":
      while classNoValid == False:
          print("Which class do you want to read? ")
          print("1. Class 1a")
          print("2. Class 1b")
          classNo = input("3. Class 1c ")
          if classNo.isnumeric() == False:
              print("That input is invalid")
              classNoValid = False
          elif classNo.isspace() == True:
              print("That input is invalid")
              classNoValid = False
          elif classNo == "":
              print("Please input a class")
              classNoValid = False
          else:
              classNoValid = True
              #^ Menu to validate class input, shown in detail earlier ^
  
      if classNo == "1":
          #Uses this option if teacher wants to see scores for class 1
          class1 = open("Class1Scores.txt", "r")
          #Opens file for class 1
          class1Array = class1.readlines()
          #Reads all lines and stores them in the variable "class1Array"
          noRecords1 = len(class1Array)
          #Finds the number of records
          for i in range (0, noRecords1):
              tempArray = class1Array[i].split(",")
              tempArray.remove("\n")
              print(tempArray)
              class1.close()
              #Loops through all records, removing the "\n" and printing it
  
          print("")
          print("")
  
      #^^ This code is mirrored for class 2 and class 3. ^^
  
  
      elif classNo == "2":
  
          class2 = open("Class2Scores.txt", "r")    
          class2Array = class2.readlines()
  
          noRecords2 = len(class2Array)
  
          for i in range (0, noRecords2):
  
              tempArray = class2Array[i].split(",")
              tempArray.remove("\n")
              print(tempArray)
              class2.close()
  
          print("")
          print("")
  
      elif classNo == "3":
  
          class3 = open("Class3Scores.txt", "r")    
          class3Array = class3.readlines()
  
          noRecords3 = len(class3Array)
  
          for i in range (0, noRecords3):
  
              tempArray = class3Array[i].split(",")
              tempArray.remove("\n")
              print(tempArray)
              class3.close()
  
          print("")
          print("")
  
  elif playOrScores == "3":
      loop = 1
      quit()
  
  else:
      print("That is an invalid entry, please enter a number between 1 and 3.")
  </code></pre>