为什么我一直得到位置参数错误?

时间:2014-01-25 18:53:01

标签: python python-3.x pygame

有人可以向我解释为什么我一直收到此错误:TypeError: get_n_nouns() takes 1 positional argument but 2 were given

通过查看类似问题(Link),我已经看过我的问题所在。但是我已经调整了我的代码以及答案,但我最终得到了上述错误。< / p>

以下是完整错误:

Traceback (most recent call last):
  File "C:/Users/...../Downloads/Comp4.1/trialTo3.py", line 21, in <module>
    app.createPhrases()
  File "C:/Users/...../Downloads/Comp4.1/trialTo3.py", line 15, in createPhrases
    words = self.get_n_nouns(1)
TypeError: get_n_nouns() takes 1 positional argument but 2 were given

以下是代码:

import csv

class apps():
    def get_n_nouns(n):
        """
        Returns the n most common nouns
        """
        with open("setPhrases.txt") as in_file:
            reader = csv.reader(in_file)
            data = [[row[0], int(row[1])] for row in list(reader)]

        return sorted(data, key=lambda x: -x[1])[:n]

    def createPhrases(self):
        words = self.get_n_nouns(1)
        for word, count in words:
            print("{}: {}".format(word, count))


app = apps()
app.createPhrases()

有人可以向我解释我哪里出错了吗?非常感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

好的,我发现了错误的位置。有点菜鸟错误。

此:

def get_n_nouns(n):

需要写成:

def get_n_nouns(self, n):

我忘了添加self部分。这就是我不断收到错误信息的原因。

相关问题