I getting an error when I try to run this function here:

def get_char_out(prompt=None):
   while True:  
     try:
        return str(raw_input(prompt + ' '))
        break
     except ValueError:
       print '\nInvaild input'

print get_char_out('String, please')

I keep getting this error here:

String, please Traceback (most recent call last):
  File "main.py", line 11, in 
    get_char_out('String, please')
  File "main.py", line 6, in get_char_out
    return str(raw_input(prompt + ' '))
EOFError: EOF when reading a line

get_char_out returns the prompt, but then I run into the EOF error. I understand that EOF has something to do with ending input, but this has me a bit stumped.

Thanks in advance!

Dani AI

Generated

Short version: an EOFError from raw_input() means Python tried to read but stdin was closed or hit end-of-file. was right to ask about the execution context, and correctly discovered the online tester was feeding an input file (so there was no interactive stdin). On terminals you can also send EOF manually (Ctrl+D on Unix/macOS, Ctrl+Z then Enter on Windows), which triggers the same exception.

Practical fixes and a robust pattern:

  • If your program should work interactively, run it from a real console (don’t double‑click on Windows), or start Python with -i so the prompt stays after the script finishes (this is what suggested).
  • If your program must also accept redirected input (like an input.txt), detect non-interactive stdin or catch EOFError and handle it cleanly. Example pattern:
import sys

def safe_input(prompt=''):
    try:
        if sys.stdin.isatty():
            return raw_input(prompt + ' ')
        # redirected input: read a line from stdin
        line = sys.stdin.readline()
        if not line:
            raise EOFError
        return line.rstrip('\n')
    except EOFError:
        return None

Keeping the console open on Windows: run the script from an already-open Command Prompt (python myscript.py) or use python -i myscript.py. As a quick hack you can add a final raw_input('Press Enter to exit') while developing (remove it for production).

Indentation gotcha: mixing tabs and spaces produces confusing IndentationErrors. Use your editor to show whitespace and convert tabs to spaces (Notepad++: View → Show Symbol → Show White Space and TAB; Edit → Blank Operations → TAB to Space). For a quick check run python -m tabnanny yourfile.py to locate inconsistent indentation.

Recommended Answers

All 4 Replies

Apparently, the stream sys.stdin was closed when your program attempted to read a line. In which context did you run the program ? Which OS first ? was it in a python IDE ? was it in a terminal ? Did you type a CTRL D in a terminal while running the program ? Was the program called from another program ?

I think i've just realize my problem. I was having problems running the code on my laptop, so I decided to test it online to see if the code was glitchly in some way, or if my python enveronment is set up wrong. Because I'm a derp, I forgot that the input file the website used (input.txt) had to be treated like a file, not a makeshift commard line. Silly me. Anyway, the code still comes up with an indention error on my laptop, but the commard box only shows for a few seconds before going off again. Any quick fix for this?

Using Notepad++, and Python 2.7.4

I don't know how you launch the program from notepad++, but if you can invoke python with option -i, it should prevent the cmd from closing.

BTW, the indent error was due to a silly mix of tabs and 4-spaces. Another bad programming habit consumed and no more!! I'm now just running code dicrectly off cmd, so that's fixed as well. All is well!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.