0

在此之前,这里列出了我为尝试了解这种情况而阅读的内容:

如何在python中检查eof

ubuntu论坛

什么是eof,它在python中的意义是什么

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

python 文档:异常

如何-一个-修复-a-python-EOF-error-when-using-raw_input

这是我的代码:

#!/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()

这是我的错误(帮助我解决其他两个错误会非常好):

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

我尝试更改代码,但没有任何反应。

4

3 回答 3

3

这通常发生在/如果您以非交互方式运行 Python 脚本时,例如通过从编辑器运行它。

请添加行

import sys
print(sys.stdin)

到你的脚本顶部并报告你得到的输出。

于 2013-05-29T13:26:34.080 回答
0

首先,您在上面的代码中有一个错字...您键入elif 'q' in menu_options():而不是elif 'q' in menu_option:. 此外,上面其他一些在运行时没有出错的原因是因为他们在定义函数后没有调用函数(这是你的代码所做的全部)。IDLE 在定义后调用之前不会评估函数的内容(语法除外)。我更正了您所做的错字,并用 pass 语句替换了您的运行、设置和退出功能,并成功运行了脚本。唯一让我出现 EOF 错误的是为 IDLE 键入文件结尾组合,在我的情况下是 CTRL-D(检查“选项”>“配置空闲”>“键”>自定义键绑定>查看“文件结尾”旁边的组合)。因此,除非您不小心按下了组合键,否则如果您的运行、设置和退出功能正常(如果您使用 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()

这是我运行的脚本...您可以尝试看看是否仍然出现该错误...

于 2013-11-30T21:12:07.030 回答
0

尝试使用 raw_input 而不是 input

于 2020-07-26T16:38:15.070 回答