为什么我在python中得到这个错误消息?缩进错误:应为缩进b

2024-06-26 02:44:42 发布

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

我是python新手,我正在尝试为我的代码创建一个简单的cicle:

for i in range(0,5) :
    if i==0 :
    b=b.append(1)

else: 
result=(b[i-1]+1) 
b.append(result)
return(result)

文件“”,第5行 b=追加(1) ^ 缩进错误:应为缩进块

如何用结果填充向量或矩阵?在


Tags: 文件代码inforreturnif错误range
2条回答

您得到的错误是因为python使用空白来确定块何时开始和结束。必须使用缩进来打开/关闭所有逻辑块。在

if thing > thing2
    inside_if_stuff = 1

the_if_has_now_ended_and_Im_doing_other_stuff 

python中没有像{}这样的字符来打开和关闭逻辑

因为你没有正确地缩进代码。它应该看起来像这样:

for i in range(0,5):
    if i==0 :
        b=b.append(1)
    else: 
        result=(b[i-1]+1) 
        b.append(result)
return(result)

在这里for循环的主体是缩进的,if和else语句的主体也是缩进的,相对于它们受尊重的语句。在

相关问题 更多 >