首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何打印所需行数

如何打印所需行数
EN

Stack Overflow用户
提问于 2016-11-20 20:25:33
回答 1查看 70关注 0票数 0

我正在尝试编写一个more函数。

我需要:

  • 使程序暂停
  • 要求打印的额外行数
  • 打印这个行数
  • 再停一次。

到目前为止,我的情况如下:

代码语言:javascript
复制
import sys, fileinput
i=0
for line in fileinput.input():
    print(line.rstrip())
    i=i+1
    if i==20:
        what=input("<--Enter the # of additional lines you wish to print/q to Quit -->")
    if what=="q":
        exit()
    else:
        if str.isdigit(what):
            print(line.rstip[i:i+int(what)]
EN

回答 1

Stack Overflow用户

发布于 2016-11-20 23:38:48

有一种方法可以做到:

代码语言:javascript
复制
with open('file.txt') as f:
    fh = f.read()
    lines = fh.splitlines()
    lines_so_far = 0
    while lines_so_far < len(lines):
        batch_count = 0
        lines_requested = int(input('How many lines to print?'))
        for line in lines:
            print(line)
            batch_count += 1
            lines_so_far += 1
            if batch_count == lines_requested and lines_so_far < len(lines):
                printmore = input ('Print more lines: Y or N?')
                if printmore == 'Y':
                    batch_count = 0
                    lines_requested = int(input('How many lines?'))
                else:
                    lines_so_far = len(lines)
                    break
    print ('\n Done Printing')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40709000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档