Indentation error: expected an indented block python 3.6

Indentation error: expected an indented block python 3.6

我是python的新手。我想 return 字符串 "shutting down" 如果 s==yes。请告诉我下面的代码有什么问题。

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

给出一个 space 或制表符。 Python 强制缩进。 ...并不意味着光标向前移动,而是您正在写一个块。

复制以下两个代码片段并尝试运行

def shut_down(s):
print "Indentation Error"

Indentation Error Demo

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

No Error Demo

tl;dr 尝试在第二行的开头添加 4 个空格,如下所示:

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

建议在 Python 中使用 4 个空格进行缩进,制表或不同数量的空格可能有效,但众所周知有时会引起麻烦。有关更多信息,请阅读 PEP8.

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

我假设您是 Python 函数定义的初学者,定义函数的正确方法是:

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

    shut_down("yes")  #calling the function

此处在函数调用期间将 "yes" 分配给 s 变量。