缩进错误:预计缩进块python 3.6

问题描述:

我是python中的新手。我想返回字符串“关闭”,如果s==yes。请告诉我在下面的代码中可能出错。缩进错误:预计缩进块python 3.6

def shut_down(s): 
... if s == "yes": 
    File "<stdin>", line 2 
    if s == "yes": 
    ^
IndentationError: expected an indented block 
+0

删除点'...'并使用'4个空格或一个制表符'作为标识。 –

+0

代码中的小点是什么?或者你混合选项卡和空间? – danche

+0

欢迎来到SO。请尝试在平台上搜索类似的问题,并在发布您的问题之前阅读python文档。 – Sadik

TL;医生尝试为4个空格添加到您这样第二行的开始:

def shut_down(s):  
    if s == "yes" 

建议在Python中使用4个空格作为缩进,列表或不同数量的空格可能有效,但也有时会引起麻烦。更多的可以在PEP8上阅读。

我不完全确定你是如何试图缩进第二行的,但重要的是要注意大多数现代Python IDE会自动替换4个空格的制表符,如果你习惯于制表符我个人使用PyCharm,但IDLE也是这样做的)。

给出空格或标签。 Python强制缩进。 ...并不意味着光标向前移动,而是写入一个块。

复制下面的两个代码片段,并尝试运行

def shut_down(s): 
print "Indentation Error" 

Indentation Error Demo

def shut_down(s): 
    print "No Indentation Error" 

No Error Demo

我假设你是在Python函数的定义非常初学者,自定义函数的正确方法是:

#function definition 
    def shut_down(s): 
     if s=="yes" : 
     print("right") 
     return 

    shut_down("yes") #calling the function 

功能在这里给你分配“是” 小号变量。

+0

文件 “”,第2行 如果s == “是”: ^ – user8164094

+0

文件 “”,第2行 如果s == “是”: ^我在第二行发生这个错误。 – user8164094

+0

高清shut_down(S): 如果s == “是”: 文件 “”,2号线 如果s == “是”: ^ IndentationError:预计缩进块 – user8164094

您需要使用4个空格进行缩进。

def shut_down(s): 
    if s == "yes":