我正在阅读本书的第一章,在编写示例代码时遇到了一个问题:
# File: chaos.py
# A simple program illustrating chaotic behavior.
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 * (i - x)
print(x)
main()
当我在jGrasp中运行此代码时,会出现以下错误:
TypeError: eval() arg 1 must be a string or code object
我不知道为什么会出现这个错误,如果有人能向我解释一下,那将非常有帮助!你知道吗
问题是您使用的是Python2,但代码要求您使用Python3。在python3中,函数
input
将返回一个字符串,而在python2中,它将计算您输入的字符串,并返回它的计算结果。你知道吗切换到python3或使用
raw_input
而不是input
。你知道吗相关问题 更多 >
编程相关推荐