1

I am doing the python tutorial by Google right now, and am completing the file list1.py.

I am supposed to fill in the def match_ends(words) part with my own code, which is supposed to count how many words in the input words have both: more than 2 letters and the same beginning and ending letters.

When I run the code I wrote using python 2.7, it works fine. But when I run it using 3.2, it doesn't. Further, when I type the line that it says has a problem into IDLE 3.2, the troublesome line runs fine.

This is list1.py:

def match_ends(words):
  count = 0
  for x in words:
    if len(x) >= 2 and x[0] == x[len(x)-1]:
        count += 1
  return count
def test(got, expected):
  if got == expected:
    prefix = ' OK '
  else:
    prefix = '  X '
  print('%s got: %s expected: %s' % (prefix, repr(got), repr(expected)))
def main():
  print('match_ends')
  test(match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']), 3)
  test(match_ends(['', 'x', 'xy', 'xyx', 'xx']), 2)
  test(match_ends(['aaa', 'be', 'abc', 'hello']), 1)
if __name__ == '__main__':
  main()

When I run it in command line for Python 2.7, it works fine, outputs:

 OK  got: 3 expected: 3
 OK  got: 2 expected: 2
 OK  got: 1 expected: 1

When I run it in command line for Python 3.2, it doesn't work, outputs:

  File "D:\Projects\Programming\Python\Tutorials\Google Python Class\google-python-exercises\basic\list1.py", line 26
    if len(x) >= 2 and x[0] == x[len(x)-1]:
                                          ^
TabError: inconsistent use of tabs and spaces in indentation

Finally, when I use IDLE 3.2, I get:

>>> def match_ends(words):
    count = 0
    for x in words:
        if len(x) >= 2 and x[0] == x[len(x)-1]:
            count += 1
    return count

>>> match_ends(["heh", "pork", "veal", "sodas"])
2

I'm extremely new to Python, most of the errors that are generated have taken some time to figure out, but I've been stuck on this one for awhile. I can't figure it out. Why won't it work in Python 3.2, and only when I do the command-line version? How do I fix this?

John Kugelman
  • 307,513
  • 65
  • 473
  • 519
Alex G
  • 737
  • 4
  • 14
  • 23
  • 2
    It looks like you might be mixing tabs and spaces in your source code. Don't do that. (You can check with `python -t` or `python -tt`) – mgilson Sep 30 '12 at 00:42
  • Where do i add this `tt`? Also, is there a difference between `-t` and `-tt`? Thanks. – Alex G Sep 30 '12 at 01:46
  • It doesn't work because you have, and I quote "inconsistent use of tabs and spaces in indentation". It's not THAT incomprehensible as an error message is it? – Lennart Regebro Oct 01 '12 at 08:18
  • I didn't realize that I could see the whitespace in the editor, so I thought I had everything at the same indentation. That's why I was confused. Once I turned on "View Whitespace", it made sense. – Alex G Oct 03 '12 at 06:56

2 Answers2

3

You're probably mixing tabs and spaces characters, which is not allowed anymore in Python 3.x. You can fix this by displaying whitespaces characters within your text editor.

Indentation is rejected as inconsistent if a source file mixes tabs and spaces in a way that makes the meaning dependent on the worth of a tab in spaces; a TabError is raised in that case.

Quoted from: http://docs.python.org/py3k/reference/lexical_analysis.html#indentation

ChristopherC
  • 1,477
  • 13
  • 29
  • Wow, so I guess I can't use tab character, then. That sucks. – Alex G Sep 30 '12 at 01:37
  • Or actually, I can use tab, but then every indentation has to be the tab key, with no 2-space indents. Man, annoying that they changed it to be more strict in reading indents. – Alex G Sep 30 '12 at 01:44
  • 1
    @AlexG No, it's annoying that people write code in a way that depends on tabs being equivalent to a certain number of spaces (because it's in general displayed differently to different people depending on editor settings). That's annoying even in languages like C where the indentation doesn't actually matter to the compiler, but only to the human reader. It's fine if you're the only person who will ever view your code, but it's a very bad habit to get into if you might ever collaborate with other programmers. – Ben Sep 30 '12 at 02:17
  • 2
    @AlexG The reason is to ensure you don't end up with misleading code. If you are using 2 space indents, and tabs are set to display as four spaces, it could look like code was indented to level 2 when it's actually to level 1. In general, the accepted Python standard is to use four spaces. – Gareth Latty Sep 30 '12 at 02:17
  • Got it. Thanks for the explanation @Lattyware. 4 spaces == 1 tab, or no? Do most people hit the space bar 4x times, or do they just tab it? – Alex G Sep 30 '12 at 03:46
  • 1
    @AlexG The original default was to have tabs align to 8-space boundaries (and many console programs still use this default). 4 spaces is more usual these days, but it's also configurable, so you can't rely on one tab's worth of indentation being equal to any particular number of spaces if your code might be viewed under different settings. Many people use editors which will insert spaces when the tab key is hit, so you sill get to use the tab key for easily typing indentation without introducing the problems of actual tab characters in the file. – Ben Sep 30 '12 at 03:59
  • 1
    @AlexG Also note that **Python itself** considers tabs to align to 8-character boundaries, not 4. http://docs.python.org/reference/lexical_analysis.html#indentation So if you indent one line with 2 spaces, the next with a tab, and a third line with 6 spaces, Python will actually think the second line is deeper than the third. **That's** why Python3 will no longer even accept that code. – Ben Sep 30 '12 at 04:22
  • Ok. I found in this post (http://stackoverflow.com/questions/455037/notepad-tabs-to-spaces) a way to change Notepad++ -- that's what I use -- to convert tabs to 4 spaces, for python only. Thanks for all the help. – Alex G Sep 30 '12 at 04:56
  • @AlexG: Most people use an editor that does not insert tabs. The Tab button is used to indent and outdent code, but no tabs are ever inserted. This is how every sane programmers editor behaves today, and if yous do not, then reconfigure it until it does, or change the editor. – Lennart Regebro Oct 01 '12 at 08:21
2

Your third line starts with two spaces, while the fourth starts with a single tab. I'm guessing Python 2 is more forgiving of inconsistencies than Python 3.

Marcelo Cantos
  • 167,268
  • 37
  • 309
  • 353