Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9289357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:09:28+00:00 2026-06-18T20:09:28+00:00

This nested loop works fine when reading lists: list = [1,2,3,4,5] num = 0

  • 0

This nested loop works fine when reading lists:

list = [1,2,3,4,5]
num = 0
while num < 5:
  for i in list:
     print(i)
     num += 1

This loop will print all elements in the list. The problem is that it doesn’t work at all when reading textfiles. Instead of printing the first 5 lines of text it will read through all and print them.

f = open(r'C:\Users\Me\Python\bible.txt')
num = 0
while num < 50:
   for line in f:
      print(line)
      num += 1

I can only assume that the num variable doesn’t increase after each iteration, is there a reason for this, and is there a solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:09:29+00:00Added an answer on June 18, 2026 at 8:09 pm

    the code

    for line in f:
        print line
        num += 1
    

    is looping over all the lines in your file. At the same time it increase num by one. So at the end of the for-loop num will be equal to the number of lines in the files, probably larger than 50, so it will exit from the while-loop.

    Using your style you should write:

    for line in f:
       print line
       num += 1
       if num > 50: break
    

    Also the first code has the same problem. Why do you need two loops if you want to loop over one structure in one dimension? Your codes are not very pythonic, for example you should rewrite them as:

    list = [1,2,3,4,5]
    for i in list:
       print i
    
    for i,line in enumerate(f):
        print line
        if i > 50: break
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two nested loop in XSL like this, at this moment I use
I am trying to do is to loop this HTML and get a nested
I have a nested list like this: <ul class=list> <li class=list_item_type_1> <ul class=list> <li
Im trying to iterate over a nestled dict list. The first level works fine.
I have this massive nested loop scenario that is calling the DB and making
I have a nested list, List<List<String>> intable; where I would like to sort all
I have 2 master pages that are nested.this is main master page code for
If you saw the railscasts on nested forms this is the helper method to
This Javascript code is using the 'this' keyword inside a nested function (which is
Is this the right way to do a nested navigation? <dl> <dt>Struktur</dt> <dd> <ul

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.