问题
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.
Here is the error in full:
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
Here is the code:
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.
回答1:
Ok so I found out where the error was. Kind of a rookie error.
This:
def get_n_nouns(n):
Needed to be written as this:
def get_n_nouns(self, n):
I had forgot to add the self
part to it. That is why I kept getting that error message.
来源:https://stackoverflow.com/questions/21354777/why-do-i-keep-getting-a-positional-argument-error