将初级Python 3摄氏度写入华氏算法时出错

2024-05-19 12:35:39 发布

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

我正在阅读本书的第一章,在编写示例代码时遇到了一个问题:

# 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

我不知道为什么会出现这个错误,如果有人能向我解释一下,那将非常有帮助!你知道吗


Tags: 代码py示例maindef错误evalprogram
4条回答

问题是您使用的是Python2,但代码要求您使用Python3。在python3中,函数input将返回一个字符串,而在python2中,它将计算您输入的字符串,并返回它的计算结果。你知道吗

切换到python3或使用raw_input而不是input。你知道吗

相关问题 更多 >