0

before anything here is a list of the stuff I've read in the effort to try to understand this situation:

how to check for eof in python

ubuntuforums

what is eof and what is its significance in python

whats-wrong-question-relied-on-files-exceptions-error-eoferror

python docs: exceptions

How-does-one-fix-a-python-EOF-error-when-using-raw_input

Here is my code:

#!/usr.bin/env python

# Errors
error1 = 'Try again'

# Functions
def menu():
    print("What would you like to do?")
    print("Run")
    print("Settings")
    print("Quit")
    # The line below is where I get the error
    menu_option = input("> ")
    if 'r' in menu_option:
        run()
    elif 's' in menu_option:
        settings()
    elif 'q' in menu_options():
        quit()
    else:
        print(error1)
        menu()

Here are my errors (helping me with the other two errors would be very nice of you ):

Traceback (innermost last):
File "C:\Program Files\Python\Tools\idle\ScriptBinding.py", line 131, in run_module_event
  execfile(filename, mod.__dict__)
File "C:\Documents and Settings\MyUser\Desktop\MyProgram.py", line 73, in ?
  menu()
File "C:\Documents and Settings\MyUser\Desktop\MyProgram.py", line 24, in menu
  menu_option = input("> ")
EOFError: EOF while reading a line

I tried changing the code but nothing happened.

Community
  • 1
  • 1
code--shitter
  • 21
  • 2
  • 2
  • 2
  • Are you using python2.x or python3.x? – mgilson May 29 '13 at 13:13
  • 2
    How are you running this? Why are you sending EOF? – Wooble May 29 '13 at 13:15
  • Your program should be interactively waiting for input; did you enter something? With an empty string as input, this might lead to this error. Also, you might want to use `raw_input()` instead of `input()` because (in Python2) the first also evaluates expressions you type. – Alfe May 29 '13 at 13:17
  • I copy-n-pasted your code into a file & ran it... but I get no errors in Python 2 or Python 3. It doesn't give me any output, but there's no errors. – Zamphatta May 29 '13 at 13:21
  • When I alter the script so I'm actually calling `main()`, I'm not getting an EOF error in Python 3, it does what it's supposed to with the `input()` and then ends up giving me a `NameError` exception after I enter the data. – Zamphatta May 29 '13 at 13:24
  • mgilson: Im using python3.3 Wooble: Im running this with IDLE Alfie: It doesn't even let me enter anything. It just displays all the text, then the '>' character and then immediately the errors. Zamphatta: I don't know why that should be. Are you running it with IDLE like I am? Also, I did not actually call main() in the code I showed you guys, but on my code which is the full program, I called it. As for the NameError exception, it should since run(), settings(), and quit() are not in this code here. They are however on my version, but still I get the errors. – code--shitter May 29 '13 at 13:28
  • 1
    Change your SO username. – Michael David Watson May 29 '13 at 13:57

3 Answers3

3

This usually happens when/if you're running a Python script in a non-interactive way, for example by running it from an editor.

Please add the lines

import sys
print(sys.stdin)

to the top of your script and report back what output you get.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • I get the same exact result – code--shitter May 29 '13 at 13:36
  • Sorry, I had a thinko there, of course you need to print `sys.stdin`. No, IDLE will open an interactive session. But other editors (like Notepad++, UltraEdit, EditPad Pro etc.) will call python.exe without a defined `stdin`, and therefore, reading from input fails. After all, if there isn't an interactive window, where would you enter your input? – Tim Pietzcker May 29 '13 at 13:42
  • Right. Ok then. What would you have me do now? Is there another way to request input in python? – code--shitter May 29 '13 at 13:44
  • Don't run your code from your editor, then. Open a `cmd` window, `cd` to your working directory and run `python.exe myscript.py` from there. Or configure your editor to open an interactive shell for you when running a Python script. – Tim Pietzcker May 29 '13 at 13:46
  • I typed the following in cmd: python.exe MyProgram.py and I get the following error: "File: "MyProgram.py", line 23 print("Run") TabError: Inconsistent use of tabs and spaces in indentation." My indentation seems to be fine. – code--shitter May 29 '13 at 13:55
  • @code--shitter: Obviously not. You're using both tabs and spaces for indentation. Which editor are you using? – Tim Pietzcker May 29 '13 at 14:06
  • I wrote my code in IDLE, but then ran the saved .py file with cmd – code--shitter May 29 '13 at 14:18
0

First of all, you have a typo in your code above...you typed elif 'q' in menu_options(): instead of elif 'q' in menu_option:. Also, the reason some of the others above got no errors while running it was because they didn't CALL the function after defining it(which is all your code does). IDLE doesn't evaluate a function's contents(except for syntax) until it's called after definition. I corrected the typo you made and replaced your run,settings and quit functions with pass statements and ran the script...successfully. The only thing that gave me an EOF error was typing the end-of-file combination for IDLE which was CTRL-D in my case(check 'Options'>'Configure Idle'>'Keys'>Custom key bindings>look at the combination next to 'end-of-file'). So, unless you accidentally pressed the key combination, your program should run alright if your run, settings and quit functions work alright(if u're using IDLE)...

#!/usr.bin/env python

error1 = 'Try again'

def menu():
    print("What would you like to do?")
    print("Run")
    print("Settings")
    print("Quit")
    # The line below is where I get the error
    menu_option = input("> ")
    if 'r' in menu_option:
        pass
    elif 's' in menu_option:
        pass
    elif 'q' in menu_option:
        pass
    else:
        print(error1)
        menu()
menu()

This was the script i ran...u can try and see if u still get that error...

SoreDakeNoKoto
  • 1,175
  • 1
  • 9
  • 16
0

try to use raw_input instead of input