0

我是初学者,遇到以下问题:每当我在 VIM 上执行以下脚本(我使用的是 Python 3.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()

我总是在最后EOFError回忆起时才得到一个main()。我得到的是:

This program illustrates a chaotic function
Enter a number between 0 and 1: Traceback (most recent call last):
  File "<stdin>", line 7, in <module>
  File "<stdin>", line 3, in main
EOFError: EOF when reading a line

并且不明白为什么,特别是因为我几周前尝试过它并且效果很好,将我扔到外壳输入值。不知道中间发生了什么或问题可能是什么。

4

2 回答 2

0

在我看来,您正在使用 Python 2 执行脚本,其中input()已经使用了eval(). 相反,要么使用 Python 3 执行脚本,要么使用 Python 2raw_input()函数。

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

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

如果您使用的是 Python 3,请替换raw_input()为。input()

于 2017-01-10T23:43:44.297 回答
0
# 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

对于 Python2使用命令python后跟文件名,对于 Python3 使用命令python3后跟文件名

Python3:

python3 example.py

Python2:

python example.py

从这里了解更多信息。

于 2017-01-11T00:07:36.727 回答