Pythonでよく出るエラーメッセージとその解決方法をまとめてみました。
TypeError: can only concatenate str (not “int”) to str
'1' + 1
python error1.py
Traceback (most recent call last):
File "error1.py", line 1, in
'1' + 1
TypeError: can only concatenate str (not "int") to str
strとintは結合できません。
'1' + '1'
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
1 + '1'
python error2.py
Traceback (most recent call last):
File "error2.py", line 1, in
1 + '1'
TypeError: unsupported operand type(s) for +: 'int' and 'str'
intとstrの結合はできません。
1 + 1
IndentationError: unexpected indent
num = 1
print(num)
python error3.py
File "error3.py", line 2
print(num)
^
IndentationError: unexpected indent
予期しないインデントが存在しています。
num = 1 print(num)
SyntaxError: EOL while scanning string literal
print('aaa)
python error4.py
File "error4.py", line 1
print('aaa)
^
SyntaxError: EOL while scanning string literal
‘や”が閉じられていません。
print('aaa')
AttributeError: ‘int’ object has no attribute ‘append’
mylist = 1 mylist.append(2)
python error5.py
Traceback (most recent call last):
File "error5.py", line 2, in
mylist.append(2)
AttributeError: 'int' object has no attribute 'append'
「’int’ object」 = 「mylist」はappend属性を持っていません。
mylist = [1] mylist.append(2)
TypeError: myfunc() missing 1 required positional argument: ‘arg2’
def myfunc(arg1, arg2):
print(arg1, arg2)
myfunc(1)
python error6.py
Traceback (most recent call last):
File "error6.py", line 4, in
myfunc(1)
TypeError: myfunc() missing 1 required positional argument: 'arg2'
必要な引数’arg2’が指定されていません。
def myfunc(arg1, arg2):
print(arg1, arg2)
myfunc(1, 2)
TypeError: myfunc() takes 2 positional arguments but 3 were given
def myfunc(arg1, arg2):
print(arg1, arg2)
myfunc(1, 2, 3)
python error7.py
Traceback (most recent call last):
File "error7.py", line 4, in
myfunc(1, 2, 3)
TypeError: myfunc() takes 2 positional arguments but 3 were given
引数2つの関数に3つの引数を与えています。
def myfunc(arg1, arg2):
print(arg1, arg2)
myfunc(1, 2)
IndexError: list index out of range
mylist = [1, 2, 3] print(mylist[3])
python error8.py
Traceback (most recent call last):
File "error8.py", line 2, in
print(mylist[3])
IndexError: list index out of range
指定したindexは存在しません。
mylist = [1, 2, 3] print(mylist[2])
KeyError: ‘d’
mydict = {'a': 1, 'b': 2, 'c': 3}
print(mydict['d'])
python error9.py
Traceback (most recent call last):
File "error9.py", line 2, in
print(mydict['d'])
KeyError: 'd'
指定したキーは存在しません。
mydict = {'a': 1, 'b': 2, 'c': 3}
print(mydict['c'])