繁体   English   中英

由于 Python 程序中的 input() ,我收到了 EOFError 。 我什至不知道为什么我有它。 我该如何解决?

[英]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?

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

如何在python中检查eof

ubuntuforums

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

什么错误问题依赖文件异常错误eoferror

python 文档:异常

How-does-one-fix-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

我尝试更改代码,但什么也没发生。

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

请添加行

import sys
print(sys.stdin)

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

首先,你在上面的代码中有一个错字......你elif 'q' in menu_options():输入了elif 'q' in menu_options():而不是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()

这是我运行的脚本......你可以尝试看看你是否仍然遇到那个错误......

尝试使用 raw_input 而不是 input

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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