原始输入多行字符串时出现python运行时错误

原始输入多行字符串时出现python运行时错误,python,input,Python,Input,我想读多行输入。输入的格式是第一行包含int作为行数,后跟字符串行。我试过使用 while True: line = (raw_input().strip()) if not line: break elif line.isdigit(): continue else: print line 它打印字符串行,但显示运行时错误消息 Traceback (most recent call last): File "prog.py", line

我想读多行输入。输入的格式是第一行包含int作为行数,后跟字符串行。我试过使用

while True:
    line = (raw_input().strip())
    if not line: break

    elif line.isdigit(): continue

    else:
        print line
它打印字符串行,但显示运行时错误消息

Traceback (most recent call last):
  File "prog.py", line 2, in <module>
    line = (raw_input().strip())
EOFError: EOF when reading a line
回溯(最近一次呼叫最后一次):
文件“prog.py”,第2行,在
行=(原始输入().strip())
EOF:读取一行时的EOF
这是读取输入的正确方法吗?
为什么运行时出错?

我是python新手,请帮助我

如果您使用EOF(Linux中为Ctrl-d,Windows中为Ctrl-z)终止程序,您可能会获得EOF。 您可以通过以下方法捕获错误:

while True:
    try:
        line = (raw_input().strip())
    except EOFError:
        break
    if not line: break

您可以执行以下操作:

while True:
    try:
        number_of_lines = int(raw_input("Enter Number of lines: ").strip())
    except ValueError, ex:
        print "Integer value for number of line" 
        continue
    except EOFError, ex:
        print "Integer value for number of line" 
        continue

    lines = []
    for x in range(number_of_lines):
        lines.append(raw_input("Line: ").strip())

    break

print lines

这将保证正确的输入

何时出现此错误?它对我来说运行得很好,我只在按下ctrl-z(EOF)时得到EOFEROR。@avasal:没必要。空字符串的计算结果为False,任何其他字符串都为True。@我也得到了junuxxEOFError@Hemc:什么时候?遵循什么输入?什么Python版本?