Crossin的编程教室

标题: Python报错typeError: 'tuple' object is not callable [打印本页]

作者: Electro01    时间: 2017-8-24 13:26
标题: Python报错typeError: 'tuple' object is not callable
  1. class Lexicon(object):

  2.     def __init__(self):
  3.         self.directions = ['north', 'south', 'east', 'west', 'down', 'up', 'left', 'right', 'back']
  4.         self.verbs = ['go', 'stop', 'kill', 'eat']
  5.         self.stops = ['the', 'in', 'of', 'from', 'at', 'it']
  6.         self.nouns = ['door', 'bear', 'princess', 'cabinet']
  7.         self.numbers = [i for i in range(10)]

  8.     def lexicon(self):
  9.         stuff = raw_input('> ')
  10.         words = stuff.split(' ')
  11.         result = self.scan(words)

  12.     def scan(self, words):
  13.         result = []
  14.         for i in words.split():
  15.             try:
  16.                 if i in self.directions:
  17.                     result.append(('direction', i))
  18.                 elif i in self.verbs:
  19.                     result.append(('verb', i))
  20.                 elif i in self.stops:
  21.                     result.append(('stop', i))
  22.                 elif i in self.nouns:
  23.                     result.append(('noun', i))
  24.                 else:
  25.                     i = int(i)
  26.                     result.append(('number', i))
  27.             except ValueError:
  28.                 result.append(('error', i))
  29.         return result

  30. lexicon = Lexicon()
复制代码
以上是我自己码的代码

以下是报错信息:
  1. sc:test admin$ nosetests
  2. ...EEEEEE
  3. ======================================================================
  4. ERROR: lexicon_tests.test_directions
  5. ----------------------------------------------------------------------
  6. Traceback (most recent call last):
  7.   File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
  8.     self.test(*self.arg)
  9.   File "/Users/admin/mystuff/test/lexicon_tests.py", line 9, in test_directions
  10.     ('direction', 'south')
  11. TypeError: 'tuple' object is not callable

  12. ======================================================================
  13. ERROR: lexicon_tests.test_verb
  14. ----------------------------------------------------------------------
  15. Traceback (most recent call last):
  16.   File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
  17.     self.test(*self.arg)
  18.   File "/Users/admin/mystuff/test/lexicon_tests.py", line 16, in test_verb
  19.     ('verb', 'kill')
  20. TypeError: 'tuple' object is not callable

  21. ======================================================================
  22. ERROR: lexicon_tests.test_stops
  23. ----------------------------------------------------------------------
  24. Traceback (most recent call last):
  25.   File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
  26.     self.test(*self.arg)
  27.   File "/Users/admin/mystuff/test/lexicon_tests.py", line 23, in test_stops
  28.     ('stop', 'in')
  29. TypeError: 'tuple' object is not callable

  30. ======================================================================
  31. ERROR: lexicon_tests.test_nouns
  32. ----------------------------------------------------------------------
  33. Traceback (most recent call last):
  34.   File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
  35.     self.test(*self.arg)
  36.   File "/Users/admin/mystuff/test/lexicon_tests.py", line 30, in test_nouns
  37.     ('noun', 'princess')])
  38. TypeError: 'tuple' object is not callable

  39. ======================================================================
  40. ERROR: lexicon_tests.test_numbers
  41. ----------------------------------------------------------------------
  42. Traceback (most recent call last):
  43.   File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
  44.     self.test(*self.arg)
  45.   File "/Users/admin/mystuff/test/lexicon_tests.py", line 36, in test_numbers
  46.     ('number', '91234')])
  47. TypeError: 'tuple' object is not callable

  48. ======================================================================
  49. ERROR: lexicon_tests.test_errors
  50. ----------------------------------------------------------------------
  51. Traceback (most recent call last):
  52.   File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
  53.     self.test(*self.arg)
  54.   File "/Users/admin/mystuff/test/lexicon_tests.py", line 42, in test_errors
  55.     ('error', 'IAS')
  56. TypeError: 'tuple' object is not callable

  57. ----------------------------------------------------------------------
  58. Ran 9 tests in 0.094s

  59. FAILED (errors=6)
  60. sc:test admin$ nosetests
  61. ...EEEEEE
  62. ======================================================================
  63. ERROR: lexicon_tests.test_directions
  64. ----------------------------------------------------------------------
  65. Traceback (most recent call last):
  66.   File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
  67.     self.test(*self.arg)
  68.   File "/Users/admin/mystuff/test/lexicon_tests.py", line 9, in test_directions
  69.     ('direction', 'south')
  70. TypeError: 'tuple' object is not callable

  71. ======================================================================
  72. ERROR: lexicon_tests.test_verb
  73. ----------------------------------------------------------------------
  74. Traceback (most recent call last):
  75.   File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
  76.     self.test(*self.arg)
  77.   File "/Users/admin/mystuff/test/lexicon_tests.py", line 16, in test_verb
  78.     ('verb', 'kill')
  79. TypeError: 'tuple' object is not callable

  80. ======================================================================
  81. ERROR: lexicon_tests.test_stops
  82. ----------------------------------------------------------------------
  83. Traceback (most recent call last):
  84.   File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
  85.     self.test(*self.arg)
  86.   File "/Users/admin/mystuff/test/lexicon_tests.py", line 23, in test_stops
  87.     ('stop', 'in')
  88. TypeError: 'tuple' object is not callable

  89. ======================================================================
  90. ERROR: lexicon_tests.test_nouns
  91. ----------------------------------------------------------------------
  92. Traceback (most recent call last):
  93.   File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
  94.     self.test(*self.arg)
  95.   File "/Users/admin/mystuff/test/lexicon_tests.py", line 30, in test_nouns
  96.     ('noun', 'princess')])
  97. TypeError: 'tuple' object is not callable

  98. ======================================================================
  99. ERROR: lexicon_tests.test_numbers
  100. ----------------------------------------------------------------------
  101. Traceback (most recent call last):
  102.   File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
  103.     self.test(*self.arg)
  104.   File "/Users/admin/mystuff/test/lexicon_tests.py", line 36, in test_numbers
  105.     ('number', '91234')])
  106. TypeError: 'tuple' object is not callable

  107. ======================================================================
  108. ERROR: lexicon_tests.test_errors
  109. ----------------------------------------------------------------------
  110. Traceback (most recent call last):
  111.   File "/Library/Python/2.7/site-packages/nose-1.3.7-py2.7.egg/nose/case.py", line 197, in runTest
  112.     self.test(*self.arg)
  113.   File "/Users/admin/mystuff/test/lexicon_tests.py", line 42, in test_errors
  114.     ('error', 'IAS')
  115. TypeError: 'tuple' object is not callable

  116. ----------------------------------------------------------------------
  117. Ran 9 tests in 0.118s

  118. FAILED (errors=6)
复制代码

作者: crossin先生    时间: 2017-8-24 13:46
报错信息:lexicon_tests.py 第42行

报错的代码你没有放上来吧
看起来是你的测试代码写的不对,并不是代码的报错
作者: Electro01    时间: 2017-8-24 14:09
crossin先生 发表于 2017-8-24 13:46
报错信息:lexicon_tests.py 第42行

报错的代码你没有放上来吧

的确是测试代码的问题,解决了! 谢谢!




欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) Powered by Discuz! X2.5