2016-11-20 3 views
0

Ich versuche, eine more Funktion zu codieren.So drucken Sie die angeforderte Anzahl von Zeilen

Ich muss:

  • das Programm Pause machen
  • fragen Sie nach der Anzahl der zusätzlichen Linien
  • drucken, die Anzahl der Zeilen
  • Pause wieder gedruckt werden.

Hier ist, was ich bisher:

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

Also, was ist das Problem mit Ihrem Code? Sagen Sie uns, was Sie erwarten würden und was stattdessen passiert. – ImportanceOfBeingErnest

Antwort

0

Hier ist eine Möglichkeit, es zu tun:

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