title: Reading a file while running a loop?
body: <p>I am having difficulty running this program without creating a logic error. I was wondering if someone could explain to me whats wrong. My code for the file WORKS:</p>
  
  <pre><code>def main():
      myfile = open('tests.txt','w')
      print('Enter six tests and scores or Enter to exit')
      print('--------------------------') #I added this feature to make the code
      #more structured
      testName = input('Enter test name: ')
      while testName != '':
          score = int(input('Enter % score of this test: ')) 
          myfile.write(str(score) + '\n')
          testName = input('Enter test name: ')  
          myfile.write(testName + '\n')
      myfile.close()
      print('File was created successfully')
  main()
  </code></pre>
  
  <p>But this code that I run to read and output the file is giving me a logic error. I know that the code is promptly written, but I don't know what is going. Can you please check my code and advise me why its not working.: this is the code</p>
  
  <pre><code>def main():
      myfile = open('tests.txt','r')
      print('Reading six tests and scores')
      print('Test\t               Score')
      print('----------------------------')
      test_score = 0
      counter = 0 #for number of tests
      line = myfile.readline()
      while line != '':
           name = line.rstrip('\n')
           score = int(myfile.readline())
           test_score += score
           print(name, score)
           line = myfile.readline()
           counter += 1
      myfile.close()
      average = test_score/ counter
      print('Average is',format(average,'.1f'))
  main()
  </code></pre>
  
  <p>Input/output for the first program should be<br>
  Entering six tests and scores
  Enter test name objects
  Enter % score on this test 88
  Enter test name loops
  Enter % score on this test 95
  Enter test name selections
  Enter % score on this test 86
  Enter test name variables
  Enter % score on this test 82
  Enter test name files
  Enter % score on this test 100
  Enter test name functions
  Enter % score on this test 80
  File was created successfully</p>
  
  <p>output for the second program that reads the file should be: </p>
  
  <p>Reading six tests and scores
  TEST          SCORE
  objects                88
  loops                   95
  selections           86
  variables             82
  files                   100
  functions             80
  Average is 88.5</p>