EOFError: EOF when reading a line – Error in Python

In this article, we will discuss the error message in Python “EOFError: EOF when reading a line”.

When does the EOFError: EOF when reading a line occur?

The above error message “EOFError: EOF when reading a line” typically occurs in Python when you try to read input from the user using the input() function, but there is no input value provided.

In Python, the input() function is used to accept user input using the standard input or stdin.

Below is given a complete error message:

Traceback (most recent call last):
  File "my_script.py", line 3, in <module>
    user_input = input("Enter a number: ")
EOFError: EOF when reading a line

The above error message refers to the line number 3 in the file my_script.py where input from the user should be taken, but no value is provided to the input.

Handling the EOFError: EOF when reading a line

We can handle the error easily using the try...except statement in Python. below is given an example to demonstrate how we can do it:

try:
    user_input = input("Your entered number is: ")
    print(user_input)
except EOFError:
    print("No user input provided. Exiting gracefully.")

In the above code, when we provide an input, will show the output as:

Your entered number is: 67

Here, the input provided by the user is 67.

If no user input is provided, it will execute the code in the except block:

Your entered number is: No user input provided. Exiting gracefully.

Leave a Reply

Your email address will not be published. Required fields are marked *