简体   繁体   中英

I get an EOFError because of an input() I have in my Python program. I don't even know why I have it. How should I fix it?

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.

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.

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...

尝试使用 raw_input 而不是 input

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM