繁体   English   中英

如何打印要求的行数

[英]How to print requested number of lines

我正在尝试编写more功能。

我需要:

  • 使程序暂停
  • 询问要打印的其他行数
  • 打印该行数
  • 再次暂停。

这是我到目前为止的内容:

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)]

这是一种实现方法:

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')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM