title: Why is my variable not defined and how do i fix this?
body: <pre><code>with open("RoundOneTotalScores.txt") as f:
      lines = f.readlines()
      values = [int(line.split(',')[1]) for line in lines]
      smallest = (min(values))
      smallest2 = (sorted(values)[:2])
      highest4 = (sorted(values)[2:])
  
  W1 = str(highest4[0])
  W2 = str(highest4[1])
  W3 = str(highest4[2])
  W4 = str(highest4[3])
  
  Myfile = open("RoundOneTotalScores.txt", "r")
  
  for line in Myfile:
      if W1 in line.split(",")[1]:
          W1L = (line.split(",")[0])
  
  for line in Myfile:
      if W2 in line.split(",")[1]:
          W2L = (line.split(",")[0])
  
  for line in Myfile:
      if W3 in line.split(",")[1]:
          W3L = (line.split(",")[0])
  
  for line in Myfile:
      if W4 in line.split(",")[1]:
          W4L = (line.split(",")[0])
  
  Myfile = open("RoundOneWinners.txt", "a")
  
  Myfile.write(W1L)
  Myfile.write(",")
  Myfile.write(W1)
  Myfile.write("\n")
  Myfile.write(W2L)
  Myfile.write(",")
  Myfile.write(W2)
  Myfile.write("\n")
  Myfile.write(W3L)
  Myfile.write(",")
  Myfile.write(W3)
  Myfile.write("\n")
  Myfile.write(W4L)
  Myfile.write(",")
  Myfile.write(W4)
  
  Myfile.close()
  </code></pre>
  
  <p>The code above does the following: opens a text file which contains six letters, each with a number. It finds the four highest of those numbers and then finds the equivalent letters. When I try to save it to a new text file, this error comes up:</p>
  
  <pre><code>Traceback (most recent call last):
  File "D:\NEA Real\Test5.py", line 37, in &lt;module&gt;
  Myfile.write(W2L)
  NameError: name 'W2L' is not defined
  </code></pre>
  
  <p>Why is W2L not defined and how do I define it?</p>