Search code examples
pythoneoferror

Python EOF error when loop


I need to write a python code to print the input like this:

while (True):
    output = raw_input()
    print output

But when I want to end the loop,I used Ctrl_D,and it says:

  File "./digits.py", line 6, in <module>
    output = raw_input()
  EOFError

How can I solve it? If possible please give me some simple way because this is the first time I write in python.


Solution

  • The EOFError is an exception that can be caught with try-except. Here we break the loop using the break keyword if an EOFError is thrown:

    while True:
        try:
            output = raw_input()
        except EOFError:
            break
    
        print(output)