2

I'm trying to add input to my python code

isAge = input("Enter your age: ")

and it shows me the following error:

enter your age: Traceback (most recent call last):
File "test2.py", line 3, in <module>
isAge = input("enter your age: ") 
EOFError: EOF when reading a line

I use "w !python" to run the code. I tried w !python 18" Where 18 is the age number, but it didnt work

1 Answer 1

4

If you use :w !python from Vim, it will start a Python interpreter and pipe the script to the interpreter, which means the Python interpreter's standard input will be connected to this pipe through which Vim will send it the script, and not to the console.

So Python functions such as input() will not work to read input from the user, since the interpreter does not have its standard input connected to the console.

To solve this, instead of having Vim pipe the script to a Python interpreter, have it invoke the Python interpreter with the script as an argument. So instead of :w !python, use:

:w
:!python %

This assumes you're editing a file and not an unnamed buffer. If you have an unnamed buffer, then save it to a *.py first.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah, now I finally understand it. Thank you so much!
I was looking for a way to test small changes without overwriting the file. but I guess the only practical answer for now is to make a working copy.

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.