Ask coding questions

← Back to all posts
add() takes 2 positional arguments but 3 were given error
RixTheTyrunt

I didn't give 3 arguments: 1 ( num1 ) and 100 ( num2 )
It should return:

101

But it returns:

Traceback (in most recent call last):
File "main.py", line 12, <module>:
print(fyf.add(1, 100))
TypeError: add() takes 2 positional arguments but 3 were given

Please help!

Answered by figglediggle [earned 5 cycles]
View Answer
Voters
figglediggle
RixTheTyrunt
Comments
hotnewtop
figglediggle

Add is a method in a class, so it must also have the self attribute passed as an argument. Example:

RixTheTyrunt

OK!

RixTheTyrunt

It works! Thank you!

RixTheTyrunt

When I added the subtract function, it says:

File "main.py", line 8
def subtract(self, num1, num2):
‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎^
IndentationError: unident does not match any outer indentation level

RixTheTyrunt

Note: The spaces before the arrow are actually "invisible characters"

figglediggle

@RixTheTyrunt That's an issue with spacing. Replace the whitespace before def subtract with either indents or spaces.

RixTheTyrunt

File "main.py, line 8
def subtract(self, num1, num2):
^
IndentationError: unident does not match any outer identation level

RixTheTyrunt

Doesn't work with that error writing using spaces instead of "invisible characters"