gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:29:02 25 4
gpt4 key购买 nike

有人可以向我解释一下为什么我不断收到此错误:TypeError: get_n_nouns() 需要 1 个位置参数,但给出了 2 个

我已经通过查看类似的问题(Link)来了解我的问题可能出在哪里,但是我已经根据答案调整了我的代码,但最终还是出现了上述错误。

以下是完整的错误:

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

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

最佳答案

好的,我知道错误出在哪里了。有点菜鸟错误。

这个:

def get_n_nouns(n):

需要写成这样:

def get_n_nouns(self, n):

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

关于python - 为什么我不断收到位置参数错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21354777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com