title: Variable resetting back to 0 after if statement in python
body: <p>I am getting this error</p>
  
  <blockquote>
    <p>IndexError: list index out of range</p>
  </blockquote>
  
  <p>and from what I've gathered it is because my <code>r</code> variable is resetting to 0 as soon as it enters the <code>if</code> statement. I'm learning python as I go so any tips and tricks would be greatly appreciated.</p>
  
  <p>Here is my code</p>
  
  <pre><code>import sys
  from test.test_email.test_message import first
  def initMatrix(B,D,seq1,seq2,gap,match,miss): 
      r=0
      c=0
      numRows=0
      numCols=0
      temp=1
      numRows=len(seq1)+1
      numCols=len(seq2)+1
      B[0][0]= 0
      D[0][0]='0'
      for r in range(numRows):
          B[r][0]=temp*gap
          temp+=temp
          D[r][0]='V'
      temp=1
      for c in range(numCols):
          B[0][c]=temp*gap
          temp+=temp
          D[0][c]='H'
      for r in range(numRows):
          for c in range(numCols):
              if seq1[r-1]==seq2[c-1]:
                  B[r][c]=match
              else:
                  B[r][c]=miss
  def initMatrixA(A,B,D,seq1,seq2,gap,match,miss):
      r,c=1,1
      first,second,third=0,0,0
      maxScore=0
      numRows=len(seq1)+1
      numCols=len(seq2)+1
      for r in range(numRows):
          for c in range(numCols):
              first=A[r-1][c-1]+B[r][c]
              second=A[r-1][c]+gap
              third=A[r][c-1]+gap
              if max(first,second,third)==first:
                  maxScore=first
                  D[r][c]='D'
              elif max(first,second,third)==second:
                  maxScore=second
                  D[r][c]='V'
              elif max(first,second,third)==third:
                  maxScore=third
                  D[r][c]='H'
  
      outputSeq(D,seq1,seq2)
  def outputSeq(D,seq1,seq2):
      r=len(seq1)
      c=len(seq2)
      i=r
      aligned1=[0 for x in range (len(seq1))]
      aligned2=[0 for x in range (len(seq2))]
      print(D)
      while D[r+1][c+1]!='0':
          if D[r+1][c+1]=='D':
              print(r)
              aligned1[i]+=seq1[r]
              aligned2[i]+=seq2[c]
              r=r-1
              c=c-1
              i=i-1
          elif D[r+1][c+1]=='V':
              print(r)
              aligned1[i]+=seq[r]
              aligned2[i]+='-'
              r=r-1
              i=i-1
          elif D[r+1][c+1]=='H':
              print(r)
              aligned1[i]='-'
              aligned2[i]=seq2[c]
              c=c-1
              i=i-1
          else: 
              r=r-1
              c=c-1
      output=open('output.a2m','w')
      output.write("&gt;Seq1"+seq1+"\n&gt;Seq2"+seq2)
      output.close()
  
  if __name__ == '__main__':
      #fasta=sys.argv[1]
      fasta=open('hw_input.fa','r')
      fasta.readline().strip()
      seq=fasta.read()
      seq1=""
      seq2=""
      s=""
      j=""
      i=0
      k=1
      gap=-2
  
      match,miss=1,-1
      with open('hw_input.fa','r') as fasta:
          for line in fasta:
              if "&gt;Seq 2" in line or i==1:
                  j=line
                  seq2+=j
                  i=1
  
  
              elif i!=1:
                  s=line
                  seq1+=s
  
  
  
      seq1=seq1.split('\n',1)[-1]
      print ("&gt;Seq1\n"+seq1)
      seq2=seq2.split('\n',1)[-1]
      print ("&gt;Seq2\n"+seq2)
      A=[[0 for x in range(len(seq1)+10)] for x in range (len(seq2)+10)]
      B=[[0 for x in range(len(seq1)+10)] for x in range (len(seq2)+10)]
      D=[[0 for x in range(len(seq1)+10)] for x in range (len(seq2)+10)]
      initMatrix(B, D, seq1, seq2, gap, match, miss)
      initMatrixA(A,B,D,seq2,seq2,gap,match,miss)
      fasta.close()
      pass
  </code></pre>
  
  <p>p.s. I am attempting to implement the Needleman-Wunchst global alignment algorithm. </p>