user1370642
user1370642

Reputation: 165

IndentationError: unindent does not match any outer indentation level in else block

def extract_names(filename):
  """
  Given a file name for baby.html, returns a list starting with the year string
  followed by the name-rank strings in alphabetical order.
  ['2006', 'Aaliyah 91', Aaron 57', 'Abagail 895', ' ...]
  """
  # +++your code here+++
  f = open(filename,'rU')
  for line in f.readlines():
      match = re.search(r'(\d\d\d\d)(</h2)',line)
      if match:
          year = match.group(1)
          print line
          print year
       else:
            match = re.search(r'<td>(\d+)</td><td)(\w+)</td><td>(\w+)</td)',line)
            if match:
                rank = match.group(1)
                boyn = match.group(2)
                girln = match.group(3)
                print rank, boyn, girln
                print year


  f.close()
  return

Getting the following error,

  ./babynames.py baby2008.html
  File "./babynames.py", line 51
    else:
        ^
IndentationError: unindent does not match any outer indentation level

Upvotes: 0

Views: 12234

Answers (2)

Mir-Ismaili
Mir-Ismaili

Reputation: 16988

Just delete one extra space character before your else: and make sure both (if and corresponding else) have exactly same indentation.

if match:
   #...
 else:    # This has only ONE EXTRA 'SPACE' at start!
   #...

Upvotes: 1

garnertb
garnertb

Reputation: 9584

The indentation in your else statement was off. Try this:

def extract_names(filename):
  """
  Given a file name for baby.html, returns a list starting with the year string
  followed by the name-rank strings in alphabetical order.
  ['2006', 'Aaliyah 91', Aaron 57', 'Abagail 895', ' ...]
  """
  # +++your code here+++
  f = open(filename,'rU')
  for line in f.readlines():
      match = re.search(r'(\d\d\d\d)(</h2)',line)
      if match:
          year = match.group(1)
          print line
          print year
      else:
          match = re.search(r'<td>(\d+)</td><td)(\w+)</td><td>(\w+)</td)',line)
          if match:
              rank = match.group(1)
              boyn = match.group(2)
              girln = match.group(3)
              print rank, boyn, girln
              print year


  f.close()
  return

Upvotes: 0

Related Questions