简体   繁体   English

Python代码在2.7中而不是在3.2中编译

[英]Python code compiles in 2.7, not in 3.2

I am doing the python tutorial by Google right now, and am completing the file list1.py. 我现在正在做Google编写的python教程,并正在完成文件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. 我应该用自己的代码填充def match_ends(words)部分,该代码应该计算输入words有多少个单词同时具有:两个以上的字母以及相同的开头和结尾字母。

When I run the code I wrote using python 2.7, it works fine. 当我运行使用python 2.7编写的代码时,它可以正常工作。 But when I run it using 3.2, it doesn't. 但是,当我使用3.2运行它时,却没有。 Further, when I type the line that it says has a problem into IDLE 3.2, the troublesome line runs fine. 此外,当我在IDLE 3.2中键入它说有问题的行时,麻烦的行会正常运行。

This is list1.py: 这是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: 当我在Python 2.7的命令行中运行它时,它工作正常,输出:

 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: 当我在Python 3.2的命令行中运行它时,它不起作用,输出:

  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: 最后,当我使用IDLE 3.2时,我得到:

>>> 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. 我是Python的新手,它所产生的大多数错误都需要花费一些时间才能弄清楚,但是我在这上面停留了一段时间。 I can't figure it out. 我不知道。 Why won't it work in Python 3.2, and only when I do the command-line version? 为什么只有在执行命令行版本时才能在Python 3.2中运行? How do I fix this? 我该如何解决?

You're probably mixing tabs and spaces characters, which is not allowed anymore in Python 3.x. 您可能混用了制表符和空格字符,这在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. 在这种情况下,将引发TabError。

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

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. 我猜想Python 2比Python 3更能容忍不一致的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM