By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,189 Members | 1,370 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,189 IT Pros & Developers. It's quick & easy.

SyntaxError: Expected Indented Block

P: 1
Below is the pasted part of my code that is coming up with the error "SyntaxError: Expected Indented Block".

Expand|Select|Wrap|Line Numbers
  1. def findRain(allData, target):
  2.     '''Uses a binary search to locate rainfall amounts in mm 
  3.     from the supplied list of dictionaries.  target is a 
  4.     date in the 'yearmonth' value format.  The function 
  5.     assumes that the list has been sorted by increasing 
  6.     date. The function will raise a ValueError exception if 
  7.     the year and month in target do not exist in allData.'''
  8.     # You must use a binary seach and cannot use any built- 
  9.     in searches.
  10.     low = 0
  11.     high = len(allData) - 1
  12.     while low <= high:
  13.         <indent> mid = (low + high) // 2
  14.         print (allData[mid]["yearmonth"])
  15.         print (target)
  16.         if target < allData[mid]["yearmonth"]:
  17.             <indent><indent>high = mid - 1
  18.         <indent> elif target > allData[mid]["yearmonth"]:
  19.             <indent><indent> low = mid + 1
  20.         <indent> else:           
  21.             <indent><indent> return (allData[mid]["rain"])   
  22.     <indent>raise ValueError("Target not found")
Edit: When it posts the indents do not show up; so each "<indent>" written indicates a "tab".
Jun 15 '18 #1
Share this Question
Share on Google+
2 Replies

P: 4
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.
Jun 15 '18 #2

P: 1
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 IndentationError: 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 .
4 Weeks Ago #3

Post your reply

Sign in to post your reply or Sign up for a free account.