2

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

4
  • When do you get this error? It runs fine for me, I only get EOFError when I press ctrl-z (EOF). Commented Feb 1, 2013 at 11:04
  • @avasal: Not necessary. Empty string evaluates as False, any other string is True. Commented Feb 1, 2013 at 11:05
  • @junuxx i am also getting EOFError Commented Feb 1, 2013 at 11:05
  • @Hemc: When? Following what input? What Python version? Commented Feb 1, 2013 at 11:06

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.