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 1085553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:42:12+00:00 2026-05-16T22:42:12+00:00

This is the code i have at the moment print Please input the filename:

  • 0

This is the code i have at the moment

print "Please input the filename:"
n = raw_input()
f = open(n,"r")
x = 0
for line in f.readlines():
    print line
    x+=1
    if x % 20 == 0:
        break
        q = raw_input()
        if q == "":
            x+= 20
        continue

Things the program should do:
1) Ask for a filename
2) Read the file
3) Print the first 20 lines of the file
4) Stop working after the first 20 lines and wait for an Enter keypress
5) If Enter is pressed show the next 20 lines of the file (20->40 and so on)

Current problem: The loop doesn’t restart, it only shows the first 20 lines and then stops working.

  • 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-05-16T22:42:12+00:00Added an answer on May 16, 2026 at 10:42 pm
    filename = raw_input("Please enter the file name: ")
    with open(filename) as f:
        lines = f.readlines()
        for i in xrange(0, len(lines), 20):
            print lines[i:i+20]
            raw_input("Press Enter for more")
    

    This reads the whole file into memory so you might not want to use slices if it’s a huge file. But if it’s a huge file, you’re unlikely to want to step through it twenty lines at a time 😉

    The major change is that we are opening the file using a with statement. This is much nicer than using open/close and guarantees that the file will always be closed.

    in the code that you posted, when you executed break, you were exiting your look. break leaves the loop so you only want to use it for that purpose. just calling a blocking operation likeraw_input` suffices if you just need to pause execution for some reason (like waiting on the user).

    also, the continue is entirely unnecessary. At the end of the body of a loop, it has no choice but to continue

    What were you thinking with the linesif q == "": x += 20? first off, it should just be if not q: x += 20 (empty string (like empty lists/dicts/tuples) evaluates to False) and second, this would skip the next 20 lines. Is this a requirement that you haven’t shared?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
So I have this code for these Constructors of the Weapon class: Weapon(const WeaponsDB
Suppose I have this code: var myArray = new Object(); myArray["firstname"] = "Bob"; myArray["lastname"]
So I have this code that takes care of command acknowledgment from remote computers,
So I have this code in a google app engine template: <select name='voter'> {%
I have this code in jQuery, that I want to reimplement with the prototype
I have this code: chars = #some list try: indx = chars.index(chars) except ValueError:
I have this code that performs an ajax call and loads the results into
I have this code #include <iostream> using namespace std; int main(int argc,char **argv) {
I have this code: CCalcArchive::CCalcArchive() : m_calcMap() { } m_calcMap is defined as this:

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.