Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-04T09:54:03.673Z
Answers.Text: Somehow I posted this on the community wiki, could someone remove it please?
Answers.UserDisplayName: M__
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-04T12:19:58.537Z
Answers.Text: Hi MIchael. I removed the CW from your post, but if something like this happens again, please flag the post for mod attention and send us a custom message explaining what you need. I only saw this comment by chance and could easily have missed it.
Answers.UserDisplayName: terdon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-05T12:44:49.783Z
Answers.Text: Hi @terdon got it, thanks.
Answers.UserDisplayName: M__
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-04T07:50:37.240Z
Answers.LastActivityDate: 2019-08-04T11:13:00.960Z
Answers.LastEditDate: 2019-08-04T11:13:00.960Z
Answers.UserDisplayName: M__
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-04T07:01:50.380Z
Answers.LastActivityDate: 2019-08-04T07:01:50.380Z
Answers.UserDisplayName: Migwell
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-08-03T16:57:16.063Z
Comments.Text: Where are you setting `maxResults`?
Comments.UserDisplayName: terdon
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-08-04T02:16:32.897Z
Comments.Text: In my case:maxResults = 3
Comments.UserDisplayName: Valse
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-08-04T06:49:34.183Z
Comments.Text: Your indentation is wrong. Python won't run this. Can you make sure you've posted exactly the code you're running?
Comments.UserDisplayName: Migwell
Tags: <python>
Title: Biopython Failure to break out of loop
ThreadText: # Biopython Failure to break out of loop
  
  (Asked by: username_0 on 2019-08-03)
  
  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
  
  ```
  
  **Tags:** python
  
  > **Comments:**
  > **username_1**: Where are you setting `maxResults`?  
  > **username_0**: In my case:maxResults = 3  
  > **username_2**: Your indentation is wrong. Python won't run this. Can you make sure you've posted exactly the code you're running?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2019-08-04. Score: 2
  
  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
  
  > **Comments:**
  > **username_3**: Somehow I posted this on the community wiki, could someone remove it please?  
  > **username_1**: Hi MIchael. I removed the CW from your post, but if something like this happens again, please flag the post for mod attention and send us a custom message explaining what you need. I only saw this comment by chance and could easily have missed it.  
  > **username_3**: Hi @username_1 got it, thanks.  
  
  ---
  
  ### Answer by username_2 on 2019-08-04. Score: 2
  
  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)
  
  ```
  
  
  ---
  
  