2016-11-20 6 views
0

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

だからあなたのコードの問題は何ですか?起こると予想されることと代わりに起こることを教えてください。 – ImportanceOfBeingErnest

答えて

0

はそれを行うための一つの方法である:

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') 
関連する問題