我想写的程序有什么问题

2024-05-19 11:31:39 发布

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

#This is a program which illustrates a chaotic behavior.


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

主()

我正在阅读《Python编程,johnzelle的计算机科学入门》一书,我试图复制粘贴这个简单的程序。 我正在使用Geany IDE,在编写了上面的代码之后,我试图编译它,但是我得到了以下错误:

Sorry: IndentationError: expected an indented block (chaos.py, line 5)

Tags: whichinputisvaluemaindefevalbetween
3条回答

错误消息准确地告诉您有什么问题:当您有一个循环(或其他代码块)时,您需要缩进它的内容。否则,解释器无法知道什么是或不是循环体的一部分

def main():
    print("This is a program which illustrates a chaotic behavior")
    x = eval(input("Enter a value between 0 and 1: "))
    for i in range(10):
        x = 3.9 * x * (1 - x) # INDENT THIS
        print(x)              # AND THIS

一定是的

for i in range(10):
    x = 3.9 * x * (1-x)
    print x

Python使用缩进对语句进行分组,例如在循环体中

缩进行

x = 3.9 * x * (1 - x)
print(x)

相关问题 更多 >

    热门问题