hemc4
hemc4

Reputation: 1633

python run time error while raw input multiple line string

I want to read multiple line input. format of input is first line contains int as no. of lines followed by string lines. i tried with

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

    elif line.isdigit(): continue

    else:
        print line

it prints the string lines but shows run time error message

Traceback (most recent call last):
  File "prog.py", line 2, in <module>
    line = (raw_input().strip())
EOFError: EOF when reading a line

Is it the right way to read input?
Why run time error?
I am new to python Please help me

Upvotes: 2

Views: 5305

Answers (2)

avasal
avasal

Reputation: 14854

you can do the following:

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

This will take care of proper inputs

Upvotes: 1

unutbu
unutbu

Reputation: 879601

You may get a EOFError if you terminate the program with an EOF (Ctrl-d in Linux, Ctrl-z in Windows). You can catch the error with:

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

Upvotes: 6

Related Questions