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

At the moment I have this code which fires a projectile: Vector3 d =
Can someone please explain this to me? I have the following code: <form action=<?php
I have this piece of code which permits me to print multiple dates, so
this is function.php of a wordpress theme that NOD32 says this code have a
I've used this code to have a like btn on my site: <iframe src=http://www.facebook.com/plugins/like.php?href=URL_OF_YOUR_WEBSITE&amp;send=false&amp;layout=standard&amp;width=450&amp;show_faces=true&amp;action=like&amp;colorscheme=light&amp;font&amp;height=80&amp
Currently I have this code: <?php echo '<meta name=robots content=noindex>'; $arr = json_decode(file_get_contents(http://media1.clubpenguin.com/play/en/web_service/game_configs/ paper_items.json),true);
So I have this code here: <table> <tr> <td width=200px valign=top> <div class=left_menu> <div
Hi I have this code and it doesn't work, what am I doing wrong?
Hello I have this code class Triplets { public: int nVal1; int nVal2; NodeT
So i have this code which makes a box, but want to make the

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.