为什么我不断收到位置参数错误? [英] Why do I keep getting a positional argument error?
问题描述
有人可以向我解释为什么我不断收到此错误:TypeError: get_n_nouns() 需要 1 个位置参数,但给出了 2 个
.
Can someone please explain to me why I keep getting this error: TypeError: get_n_nouns() takes 1 positional argument but 2 were given
.
我已经通过查看类似的问题(链接) 但是我已经根据答案调整了我的代码,但最终还是出现了上述错误.
I have already had a look at where my problem may be by looking at a similar question (Link) But I have adapted my code going along with the answer and yet I end up with the above error.
这里是完整的错误:
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()
有人可以向我解释我哪里出错了吗?非常感谢任何帮助.
Can someone please explain to me where I am going wrong? Any help is much appreciated.
推荐答案
好的,我找到了错误所在.有点菜鸟错误.
Ok so I found out where the error was. Kind of a rookie error.
这个:
def get_n_nouns(n):
需要这样写:
def get_n_nouns(self, n):
我忘记添加 self
部分.这就是我不断收到错误消息的原因.
I had forgot to add the self
part to it. That is why I kept getting that error message.
这篇关于为什么我不断收到位置参数错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!