Python Forum
Unable to execute input(). - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Unable to execute input(). (/thread-32544.html)



Unable to execute input(). - jahuja73 - Feb-17-2021

Unable to run the below code.

# your code goes here
def get_input():
    my_var = str(input('Enter "a" or "b": '))
    if my_var == "a" or my_var == "b":
        print('got input:', my_var)
        return my_var
    else:
        print('You didn\'t type "a" or "b". Try again.')
        return get_input()


def get_input1():
    my_var = input('Enter "a" or "b": ')

    if my_var != "a" and my_var != "b":
        print('You didn\'t type "a" or "b". Try again.')
        return get_input1()
    else:
        return my_var

print('got input:', get_input1())

#get_input()
Get error:

Traceback (most recent call last):
File "prog.py", line 21, in <module>
File "prog.py", line 13, in get_input1
EOFError: EOF when reading a line


RE: Unable to execute input(). - Serafim - Feb-17-2021

(Feb-17-2021, 07:01 AM)jahuja73 Wrote: Unable to run the below code.

# your code goes here
def get_input():
    my_var = str(input('Enter "a" or "b": '))
    if my_var == "a" or my_var == "b":
        print('got input:', my_var)
        return my_var
    else:
        print('You didn\'t type "a" or "b". Try again.')
        return get_input()


def get_input1():
    my_var = input('Enter "a" or "b": ')

    if my_var != "a" and my_var != "b":
        print('You didn\'t type "a" or "b". Try again.')
        return get_input1()
    else:
        return my_var

print('got input:', get_input1())

#get_input()
Get error:

Traceback (most recent call last):
File "prog.py", line 21, in <module>
File "prog.py", line 13, in get_input1
EOFError: EOF when reading a line

I don't get any errors when running that code. Neither when running it as is or when changing it to use the function "get_input()". Not perfect, but it runs without errors.


RE: Unable to execute input(). - jahuja73 - Feb-17-2021

(Feb-17-2021, 07:53 AM)Serafim Wrote: I don't get any errors when running that code. Neither when running it as is or when changing it to use the function "get_input()". Not perfect, but it runs without errors.
I tried online at :
https://ideone.com/EulG3N

Please tell if it is a site (platform) based error on execution.


RE: Unable to execute input(). - bowlofred - Feb-17-2021

You'll get that EOFError: EOF when reading a line error if input() gets an "end-of-file" condition. Perhaps by being redirected to an empty/unreadable file, terminated with a control-D/control-Z, or problems connecting to the correct input device.

Output:
$ python -c 'input("type input:")' type input:k
But when redirected so that no data can be read:

Output:
$ python -c 'input("type input:")' < /dev/null type input:Traceback (most recent call last): File "<string>", line 1, in <module> EOFError: EOF when reading a line



RE: Unable to execute input(). - jahuja73 - Feb-18-2021

(Feb-17-2021, 08:51 AM)bowlofred Wrote: You'll get that EOFError: EOF when reading a line error if input() gets an "end-of-file" condition. Perhaps by being redirected to an empty/unreadable file, terminated with a control-D/control-Z, or problems connecting to the correct input device.

Output:
$ python -c 'input("type input:")' type input:k
But when redirected so that no data can be read:

Output:
$ python -c 'input("type input:")' < /dev/null type input:Traceback (most recent call last): File "<string>", line 1, in <module> EOFError: EOF when reading a line

But, request a way to remove the error on ideone.com, as usually run there only.

It would help in future too for taking an input.


RE: Unable to execute input(). - bowlofred - Feb-18-2021

Looks like you can't do interactive stuff on ideone. But you can type in the input ahead of time.

Below your code should be another window with the "input" tab. Type in your answers there and it will feed it to the program when you run.