text: user4999: After qblast, I wanted to print only details from the first 3 BLAST output. Yet, the code below printed the details of every BLAST output. Why can't I break out of the loop to achieve my desired output?
  for alignment in blast_records.alignments:
      countResults = 0
      for hsp in alignment.hsps:  
          if countResults == maxResults:
              countResults = 0
          break
          else:
              print(alignment.title)
              print(alignment.length/ len(seq)*100)
              print(hsp.expect)
              print(hsp.identities)
              countResults += 1
  
  
  user4645: Assuming that your actual code is this:
  for alignment in blast_records.alignments:
      countResults = 0
      for hsp in alignment.hsps:  
          if countResults == maxResults:
              countResults = 0
              break
          else:
              print(alignment.title)
              print(alignment.length/ len(seq)*100)
              print(hsp.expect)
              print(hsp.identities)
              countResults += 1
  
  Here you have an outer loop, which is iterating over all the possible database matches, and an inner loop, iterating over the alignments within that database entry. You are currently limiting the inner loop, but not the outer loop, which you probably want to do. Try this:
  # Take the first 3 database hits
  for alignment in blast_records.alignments[:3]:
      for hsp in alignment.hsps:  
              print(alignment.title)
              print(alignment.length/ len(seq)*100)
              print(hsp.expect)
              print(hsp.identities)
  
  
  user3848: There was an indentation bug (its a Python thing). Personally I'd use,
  for alignment in blast_records.alignments:
      countResults = 0
      maxResults = 3
      for hsp in alignment.hsps:  
          if not countResults == maxResults:
              print(alignment.title)
              print(alignment.length/ len(seq)*100)
              print(hsp.expect)
              print(hsp.identities)
              countResults += 1
          else:
              countResults = 0 # I'd dedinitely remove this in your original code
              break
  
  The loop gives a bit more control if there is any subsequent filtering, species, percentage identity etc.. There are other ways to write it.
  Here's an alternative
  for alignment in blast_records.alignments:
      maxResults = 3
      for countResults, hsp in enumerate(alignment.hsps):
          while countResults <= maxResults:                
              print(\
              alignment.title, "\n",\
              alignment.length/ len(seq)*100, "\n",\
              hsp.expect, "\n", hsp.identities, "\n"\
              )
  
  BTW the enumerate function keeps track of the counting.
  Alternative print ... 
             out = """ alignment.title,
             alignment.length/ len(seq)*100, 
             hsp.expect, 
             hsp.identities """
             print (out)
  
  Let me know if there are bugs
  
original_text: 4999: <p>After qblast, I wanted to print only details from the first 3 BLAST output. Yet, the code below printed the details of every BLAST output. Why can't I break out of the loop to achieve my desired output?</p>
  
  <pre><code>for alignment in blast_records.alignments:
      countResults = 0
      for hsp in alignment.hsps:  
          if countResults == maxResults:
              countResults = 0
          break
          else:
              print(alignment.title)
              print(alignment.length/ len(seq)*100)
              print(hsp.expect)
              print(hsp.identities)
              countResults += 1
  </code></pre>
   4645: <p>Assuming that your actual code is this:</p>
  
  <pre><code>for alignment in blast_records.alignments:
      countResults = 0
      for hsp in alignment.hsps:  
          if countResults == maxResults:
              countResults = 0
              break
          else:
              print(alignment.title)
              print(alignment.length/ len(seq)*100)
              print(hsp.expect)
              print(hsp.identities)
              countResults += 1
  </code></pre>
  
  <p>Here you have an outer loop, which is iterating over all the possible database matches, and an inner loop, iterating over the alignments within that database entry. You are currently limiting the inner loop, but not the outer loop, which you probably want to do. Try this:</p>
  
  <pre><code># Take the first 3 database hits
  for alignment in blast_records.alignments[:3]:
      for hsp in alignment.hsps:  
              print(alignment.title)
              print(alignment.length/ len(seq)*100)
              print(hsp.expect)
              print(hsp.identities)
  </code></pre>
   3848: <p>There was an indentation bug (its a Python thing). Personally I'd use,</p>
  
  <pre><code>for alignment in blast_records.alignments:
      countResults = 0
      maxResults = 3
      for hsp in alignment.hsps:  
          if not countResults == maxResults:
              print(alignment.title)
              print(alignment.length/ len(seq)*100)
              print(hsp.expect)
              print(hsp.identities)
              countResults += 1
          else:
              countResults = 0 # I'd dedinitely remove this in your original code
              break
  </code></pre>
  
  <p>The loop gives a bit more control if there is any subsequent filtering, species, percentage identity etc.. There are other ways to write it.</p>
  
  <p>Here's an alternative</p>
  
  <pre><code>for alignment in blast_records.alignments:
      maxResults = 3
      for countResults, hsp in enumerate(alignment.hsps):
          while countResults &lt;= maxResults:                
              print(\
              alignment.title, "\n",\
              alignment.length/ len(seq)*100, "\n",\
              hsp.expect, "\n", hsp.identities, "\n"\
              )
  </code></pre>
  
  <p>BTW the enumerate function keeps track of the counting.</p>
  
  <p>Alternative print ... </p>
  
  <pre><code>           out = """ alignment.title,
             alignment.length/ len(seq)*100, 
             hsp.expect, 
             hsp.identities """
             print (out)
  </code></pre>
  
  <p>Let me know if there are bugs</p>
  