为什么我不断收到位置参数错误?

2024-02-01

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

我已经通过查看类似的问题了解了我的问题可能出在哪里(Link https://stackoverflow.com/questions/18884782/typeerror-worker-takes-0-positional-arguments-but-1-was-given)但是我已经根据答案调整了我的代码,但最终还是出现了上述错误。

这是完整的错误:

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()

有人可以向我解释一下我哪里出错了吗?任何帮助深表感谢。


好吧,我发现错误出在哪里了。有点菜鸟错误。

This:

def get_n_nouns(n):

需要写成这样:

def get_n_nouns(self, n):

我忘了添加self一部分。这就是为什么我不断收到该错误消息的原因。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我不断收到位置参数错误? 的相关文章

随机推荐