SyntaxError: Expected Indented Block

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • erika0193
    New Member
    • Jun 2018
    • 1

    SyntaxError: Expected Indented Block

    Below is the pasted part of my code that is coming up with the error "SyntaxErro r: Expected Indented Block".

    Code:
    def findRain(allData, target):
        '''Uses a binary search to locate rainfall amounts in mm 
        from the supplied list of dictionaries.  target is a 
        date in the 'yearmonth' value format.  The function 
        assumes that the list has been sorted by increasing 
        date. The function will raise a ValueError exception if 
        the year and month in target do not exist in allData.'''
        # You must use a binary seach and cannot use any built- 
        in searches.
        low = 0
        high = len(allData) - 1
        while low <= high:
            <indent> mid = (low + high) // 2
            print (allData[mid]["yearmonth"])
            print (target)
            if target < allData[mid]["yearmonth"]:
                <indent><indent>high = mid - 1
            <indent> elif target > allData[mid]["yearmonth"]:
                <indent><indent> low = mid + 1
            <indent> else:           
                <indent><indent> return (allData[mid]["rain"])   
        <indent>raise ValueError("Target not found")
    Edit: When it posts the indents do not show up; so each "<indent>" written indicates a "tab".
    Last edited by RonB; Jun 15 '18, 02:40 PM. Reason: Added code tags
  • husoski
    New Member
    • Feb 2017
    • 4

    #2
    You have begun a multi-line string literal for your docstring (starting with ''') but left out the closing '''. At the end of line 9, add that ''', or insert a line afterward consisting of just the ''' and nothing else. (My preference is to indent it 4 spaces to line up with the text above, but that's optional.)

    I can't tell if there are other problems, since the code hasn't pasted correctly. Those "<insert>" tokens look like some sort of markup.
    Last edited by husoski; Jun 15 '18, 10:51 PM. Reason: Add remark about <insert> tags in question.

    Comment

    • evanbung
      New Member
      • Mar 2020
      • 1

      #3
      Putting in an extra space or leaving one out where it is needed will surely generate an error message . Some common causes of this error include:
      • Forgetting to indent the statements within a compound statement
      • Forgetting to indent the statements of a user-defined function.


      The error message IndentationErro r: expected an indented block would seem to indicate that you have an indentation error. It is probably caused by a mix of tabs and spaces. The indentation can be any consistent white space . 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. Tabs are a bad idea because they may create different amount if spacing in different editors .
      Last edited by Rabbit; Mar 2 '20, 05:40 PM. Reason: external link removed

      Comment

      Working...