Indentation error: expected an indented block python 3.6 [closed]

馋奶兔 提交于 2019-12-11 05:28:17

问题


I am a newbie in python. I wanted to return string "shutting down" if s==yes. Pls tell me what could be wrong in the below code.

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

回答1:


tl;dr try to add 4 spaces to the start of your second line like this:

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

It is recommended to use 4 spaces for indentation in Python, tabulation or a different number of spaces may work, but it is also known to cause trouble at times. more on that can be read on PEP8.

I'm not exactly sure how you tried to indent your second line, but it is important to note that most modern Python IDEs will automatically replace your tabs with 4 spaces, which is great if you're used to tabbing (I personally use PyCharm, but IDLE does that as well).




回答2:


I am assuming you are a very beginner in Python function definition , The correct way to define function is :

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

    shut_down("yes")  #calling the function

Here during function call you assign the "yes" to s variable.




回答3:


Give a space or tab. Python enforces indentation. ... does not means that cursor is moved ahead but that you are writing for a block.

Copy following two code snippet and try running

def shut_down(s):
print "Indentation Error"

Indentation Error Demo

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

No Error Demo



来源:https://stackoverflow.com/questions/44612393/indentation-error-expected-an-indented-block-python-3-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!