IndentationError expected an indented block

后端 未结 5 1886
借酒劲吻你
借酒劲吻你 2020-12-05 18:30

Here is the code:

def myfirst_yoursecond(p,q):

a = p.find(" ")
b = q.find(" ")
str_p = p[0:a]
str_q = p[b+1:]

if str_p == str_q:
    res         


        
相关标签:
5条回答
  • 2020-12-05 19:15

    I got the same error, This is what i did to solve the issue.

    Before Indentation:

    Indentation Error: expected an indented block.

    After Indentation:

    Working fine. After TAB space.

    0 讨论(0)
  • 2020-12-05 19:16

    You should install a editor (or IDE) supporting Python syntax. It can highlight source code and make basic format checking. For example: Eric4, Spyder, Ninjia, or Emacs, Vi.

    0 讨论(0)
  • 2020-12-05 19:28

    You've mixed tabs and spaces. This can lead to some confusing errors.

    I'd suggest using only tabs or only spaces for indentation.

    Using only spaces is generally the easier choice. Most editors have an option for automatically converting tabs to spaces. If your editor has this option, turn it on.


    As an aside, your code is more verbose than it needs to be. Instead of this:

    if str_p == str_q:
        result = True
    else:
        result = False
    return result
    

    Just do this:

    return str_p == str_q
    

    You also appear to have a bug on this line:

    str_q = p[b+1:]
    

    I'll leave you to figure out what the error is.

    0 讨论(0)
  • 2020-12-05 19:31

    This error also occurs if you have a block with no statements in it

    For example:

    def my_function():
        for i in range(1,10):
    
    
    def say_hello():
        return "hello"
    

    Notice that the for block is empty. You can use the pass statement if you want to test the remaining code in the module.

    0 讨论(0)
  • 2020-12-05 19:32

    If you are using a mac and sublime text 3, this is what you do.

    Go to your /Packages/User/ and create a file called Python.sublime-settings.

    Typically /Packages/User is inside your ~/Library/Application Support/Sublime Text 3/Packages/User/Python.sublime-settings if you are using mac os x.

    Then you put this in the Python.sublime-settings.

    {
        "tab_size": 4,
        "translate_tabs_to_spaces": false
    }
    

    Credit goes to Mark Byer's answer, sublime text 3 docs and python style guide.

    This answer is mostly for readers who had the same issue and stumble upon this and are using sublime text 3 on Mac OS X.

    0 讨论(0)
提交回复
热议问题