在VIM上使用Python3代码时遇到EOFError

2024-05-19 12:25:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我是个初学者,遇到了以下问题:每当我在VIM上执行以下脚本(我使用的是Python3.6):

def main():
    print("This program illustrates a chaotic function")
    x=eval(input("Enter a number between 0 and 1: "))
    for i in range(10):
        x=3.9*x*(1-x)
        print(x)
main()

我总是只有在我回忆起结尾的main()时才会得到一个EOFError。我得到的是:

^{pr2}$

我也不明白为什么,特别是因为我几周前试过了,效果很好,把我扔到shell上输入值。不知道中间发生了什么事,也不知道问题是什么。在


Tags: 脚本numberinputmaindefevalfunctionvim
2条回答
# For Python2
# Replace the line:
x=eval(input("Enter a number between 0 and 1: "))
# with:
x=input("Enter a number between 0 and 1: ")

# For Python3, the line:
x=eval(input("Enter a number between 0 and 1: "))
# should work

使用命令python后跟Python2的文件名,使用命令python3后跟python3的文件名

Python3:

^{pr2}$

Python2:

python example.py

here了解更多信息。在

听起来像是用python2执行脚本,^{}已经使用了eval()。相反,要么用python3执行脚本,要么使用python2^{}函数。在

另外,您不需要使用eval将用户输入的浮点字符串转换为实际的浮点;只需使用float()

x = float(raw_input("Enter a number between 0 and 1: "))

如果您使用的是python3,请将raw_input()替换为input()。在

相关问题 更多 >