FrostL
FrostL

Reputation: 15

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.

Upvotes: 1

Views: 4055

Answers (2)

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)

Upvotes: 5

FrenchDwarf
FrenchDwarf

Reputation: 21

Have you considered putting a keyword check in your loop?

while (True):
    output = raw_input()
    if str(output) == "exit":
        break
    print output

Upvotes: 0

Related Questions