中級 › TypeError: takes N positional arguments but M were given の原因と直し方 › パターン1
TypeError: takes N positional arguments but M were given の原因と直し方
関数に定義されている数より多くの引数を渡したときに出るエラーです。
1 def add(a, b): 2 return a + b 3 4 print(add(_______)) ^
Traceback (most recent call last):
File "main.py", line 4, in <module>
print(add(1, 2, 3))
^^^^^^^^^^^^
TypeError: add() takes 2 positional arguments but 3 were given
空欄を埋めて実行を通す
定義した引数の数より多く渡すとエラーになります。多い分の値を受け取る仕組み(*args)が無い限り、数はぴったり合わせる必要があります。
次の問題