TYPEERROR

Python TypeError の直し方|エラー文の英語から原因を逆引き

'str' object cannot be interpreted as an integer・can only concatenate str (not "int") to str など、表示された英語から原因と直し方を引けます。

🎯 対象: TypeError で止まった初心者 ⏱️ 読了: 約9分 🧪 実測: Windows 11・Python 3.15.2/3.14.7/3.12.10・2026-11-24

結論:TypeError は「値の型が、その場所で使えない」ときに出ます。最終行の英語を下の表で探し、エラーの行で使っている値を print(x, type(x)) で確かめてください。

1. 結論:メッセージ別の早見表

エラー文よくある原因直し方
'str' object cannot be interpreted as an integerinput() の値を range() に渡したint() で変換
'NoneType'・'tuple'・'list'・'float' object cannot be interpreted as an integerreturn 忘れ・末尾のカンマ・len() 忘れ渡す値を整数にする
can only concatenate str (not "int") to str文字列 + 数値str() か f-string
unsupported operand type(s) for +: 'int' and 'str'数値 + input() の値int() で変換
input expected at most 1 argument, got 2input() にカンマで2つ渡したf-string で1つにまとめる
list indices must be integers or slices, not tuplegrid[0, 1]・リストのカンマ抜けgrid[0][1]・カンマを足す
'NoneType' object is not subscriptablesort() の戻り値を代入した代入せずに sort() だけ書く
'int' object is not iterablefor i in 5・for i in len(lst)range() で囲む
can only join an iterablejoin() に None や数値リストを渡す
can only assign an iterable(今回の3版では must assign iterable to extended slice)スライスに値を1つだけ代入[9] のようにリストで渡す
'str' object is not callablestr・input を変数名にした変数名を変える
add() takes 2 positional arguments but 3 were given引数が多い・self 忘れ定義と呼び出しの数をそろえる
missing 1 required positional argument引数が足りない足りない引数を渡す
unhashable type: 'list'リストを辞書のキーにしたタプルにする
a bytes-like object is required, not 'str'bytes に str を混ぜたdecode() してから使う

表のエラー文は Windows 11 の Python 3.15.2・3.14.7・3.12.10 で実行して出したものです(can only assign an iterable だけは参考ビルドの 3.11.13)。版で最終行が違ったのは2つだけで、7章・10章に分けました。本文の矢印は 3.14.7 の表示です。

2. 'str' object cannot be interpreted as an integer の直し方

range() など整数しか受け取らない場所に、文字列を渡しています。input() の値は数字を打っても文字列です(基本文法の input())。

n = input("回数: ")   # 3 と入力
for i in range(n):
    print(i)
    for i in range(n):
             ~~~~~^^^
TypeError: 'str' object cannot be interpreted as an integer

int() で整数にしてから渡します。3 を入力すると 0・1・2 と表示されました。

n = int(input("回数: "))

変換した次に出るのは ValueError です。abc を入力すると ValueError: invalid literal for int() with base 10: 'abc' になりました。直し方はエラー辞典の ValueError にあります。

'NoneType'・'tuple'・'list'・'float' object cannot be interpreted as an integer

引用符の中の型名が、実際に渡した値の型です。

表示の型名出たコード直し方
'NoneType'range(get_count())。関数に return が無い関数の最後に return count
'tuple'n = 3, や return 3,。末尾のカンマでタプルになるカンマを消す
'list'range(fruits)。リストそのものを渡したrange(len(fruits))
'float'range(10 / 2)。/ の結果は小数10 // 2 か int()

型名が予想と違うときは、print(n, type(n)) を1行足すと原因が見えます。

3. can only concatenate str (not "int") to str の直し方

文字列に数値を + でつないでいます。

age = 25
print("年齢は" + age + "歳")
    print("年齢は" + age + "歳")
          ~~~~~~~~~^~~~~
TypeError: can only concatenate str (not "int") to str

str(age) で文字列にするか、f-string で埋め込みます。どちらも 年齢は25歳 と表示されました。

print("年齢は" + str(age) + "歳")
print(f"年齢は{age}歳")

括弧の中は混ざった値の型です。None をつなぐと (not "NoneType") になりました。

unsupported operand type(s) for +: 'int' and 'str'

左が数値、右が文字列のときはこちらの表示です。input() の値で計算すると出ます。

x = input("数: ")   # 5 と入力
print(1 + x)        # TypeError
print(1 + int(x))   # 6

4. input expected at most 1 argument, got 2 の直し方

input() に渡せる表示用の文字列は1つだけです。print() と同じようにカンマで並べると出ます。

name = "太郎"
age = input(name, "さんの年齢: ")
    age = input(name, "さんの年齢: ")
          ~~~~~^^^^^^^^^^^^^^^^^^^^^^
TypeError: input expected at most 1 argument, got 2

f-string で1つの文字列にまとめます。太郎さんの年齢: と表示されて入力を待ちました。list(1, 2) も同じ形の list expected at most 1 argument, got 2 でした。[1, 2] と書きます。

age = input(f"{name}さんの年齢: ")

5. list indices must be integers or slices, not tuple の直し方

リストの [] の中にカンマを書いています。[0, 1] の中身はタプルとして渡ります。

grid = [[1, 2], [3, 4]]
print(grid[0, 1])   # TypeError
print(grid[0][1])   # 2

2次元のリストは [0][1] と2回に分けて書きます。[0, 1] の書き方が通るのは NumPy の配列で、np.array(grid)[0, 1] は 2 でした。

行の間のカンマ抜けでも同じエラーです。次のコードは実行前に警告が出ました。

grid = [
    [1, 2]
    [3, 4]
]
SyntaxWarning: list indices must be integers or slices, not tuple; perhaps you missed a comma?

[1, 2] の行末にカンマを足します。警告の perhaps you missed a comma? がそのまま答えです。カンマ抜けの他の例は Perhaps you forgot a comma? の節にあります。

not str なら、input() の値を添字に使っています。lst[int(i)] と変換します。

6. 'NoneType' object is not subscriptable の直し方

None に [0] を付けています。初心者が出しやすいのは sort() の戻り値を代入する書き方です。

scores = [70, 90, 80]
scores = scores.sort()
print(scores[0])
    print(scores[0])
          ~~~~~~^^^
TypeError: 'NoneType' object is not subscriptable

sort() はリストそのものを並べ替え、並べ替えたリストを返しません(公式:list.sort)。

scores.sort()                  # 代入しない
print(scores[0])               # 70
print(sorted([70, 90, 80])[0]) # 新しいリストが要るなら sorted()

return の無い関数に load()["a"] と書いても同じ表示でした。return の書き方を確かめてください。

scores.sort(True) は sort() takes no positional arguments でした。降順なら sort(reverse=True) と名前を付けて渡します。

7. 'int' object is not iterable・can only join an iterable の直し方

for で数値をそのまま回しています。for i in 5: でも for i in len(lst): でも出ました。

    for i in n:
             ^
TypeError: 'int' object is not iterable

回数だけ繰り返すなら for i in range(n):、リストの中身なら for x in lst: です(for 文の書き方)。

TypeError: can only join an iterable

",".join() に、リストではなく None や数値を渡しています。

words = ["b", "a"]
print(",".join(words.sort()))   # TypeError: can only join an iterable
words.sort()
print(",".join(words))          # a,b

数値のリストを渡すと別の表示です。",".join([1, 2, 3]) は sequence item 0: expected str instance, int found でした。",".join(map(str, [1, 2, 3])) で 1,2,3 になります。

TypeError: can only assign an iterable

スライスに値を1つだけ代入したときの表示です。今回の3版では文が違いました。

版lst[0:2] = 9 の表示
3.15.2・3.14.7・3.12.10must assign iterable to extended slice
3.11.13(python.org 外のビルド・参考)can only assign an iterable

どちらも直し方は同じです。lst[0:2] = [9] とリストで渡すと [9, 3, 4] になりました。どの版で文が変わったかは確認できていません。

8. 'str' object is not callable の直し方

組み込み関数と同じ名前の変数を作り、関数を上書きしています。

str = "こんにちは"
print(str(10))
    print(str(10))
          ~~~^^^^
TypeError: 'str' object is not callable

変数名を message などに変えます。input = "a" の後の input() も同じでした。

'int' object is not callable は、a(b + 1) のように掛け算の * を抜いたときに出ました。'list' object is not callable は、lst(0) と [] の代わりに () を書いたときです。

9. takes 2 positional arguments but 3 were given の直し方

関数の定義より多い引数を渡しています。

def add(a, b):
    return a + b

add(1, 2, 3)   # TypeError: add() takes 2 positional arguments but 3 were given

呼び出しを add(1, 2) にするか、定義を def add(a, b, c): にします。引数の書き方は関数の引数の解説にあります。

引数を1つも渡していないのに but 1 was given なら、self の書き忘れです。

class Dog:
    def bark():        # def bark(self): が正しい
        print("ワン")

Dog().bark()   # TypeError: Dog.bark() takes 0 positional arguments but 1 was given

メソッドには、インスタンスが最初の引数として自動で渡されます(クラスの基本)。

missing 1 required positional argument の直し方

引数が足りないときの表示です。末尾に足りない引数の名前が出ます。

コード表示
add(1)add() missing 1 required positional argument: 'b'
Dog.bark()(Dog の後の () 忘れ)Dog.bark() missing 1 required positional argument: 'self'
Dog()(__init__(self, name) のクラス)Dog.__init__() missing 1 required positional argument: 'name'

'self' と出たら、Dog().bark() のようにインスタンスを作ってから呼びます。

array() takes from 1 to 2 positional arguments but 3 were given

NumPy の np.array(1, 2, 3) で出ました(Python 3.12.10+NumPy 2.4.4、3.14.7+NumPy 2.5.5)。値を角括弧で1つのリストにします。

import numpy as np
a = np.array([1, 2, 3])
b = np.array([[1, 2], [3, 4]])   # 2次元は外側も [] で囲む

3.15.2 では NumPy を試していません。

10. unhashable type: 'list' の直し方

リストを辞書のキーや集合の要素にしています。中身を変えられる型はキーにできません。3.14 から表示が変わりました。

コード3.15.2・3.14.73.12.10
d[[1, 2]] = "a"cannot use 'list' as a dict key (unhashable type: 'list')unhashable type: 'list'
{[1, 2]}cannot use 'list' as a set element (unhashable type: 'list')unhashable type: 'list'
s.add({"a": 1})cannot use 'dict' as a set element (unhashable type: 'dict')unhashable type: 'dict'

この変更は公式の What's New 3.14 に載っています。

キーにするならタプルに変えます。d[(1, 2)] = "a" は {(1, 2): 'a'} になりました。

11. a bytes-like object is required, not 'str' の直し方

bytes(b"...")の操作に、ふつうの文字列を渡しています。

data = b"a,b,c"
print(data.split(","))                  # TypeError
print(data.decode("utf-8").split(","))  # ['a', 'b', 'c']
print(data.split(b","))                 # [b'a', b'b', b'c']

文字列として扱うなら decode() で str にします。bytes のまま使うなら区切りも b"," にします。"wb" で開いたファイルへの f.write("abc") でも同じ表示でした。

decode() で別のエラーが出たときは UnicodeDecodeError の直し方を見てください。

12. Traceback (most recent call last) の読み方

見るのは最終行と、その直前のコードの行です。関数の中で出ると、呼び出した順に上から並びます。

Traceback (most recent call last):
  File "C:\...\main.py", line 7, in <module>
    print(total([100, "200"]))
          ~~~~~^^^^^^^^^^^^^^
  File "C:\...\main.py", line 4, in total
    s = s + p
        ~~^~~
TypeError: unsupported operand type(s) for +: 'int' and 'str'
  • 最終行:エラーの種類と説明。この記事の表で引く文
  • 一番下の File:エラーが起きた場所。4行目の s + p で、p が文字列の "200"
  • 上の File:そこを呼んだ場所。直すのはデータを作った7行目のこともある

~ と ^ は版で違いました(3.12.10 は total(...) の行全体が ^、add(1, 2, 3) は印なし)。最終行は同じです。

Jupyter で出る「TypeError Traceback (most recent call last)」

IPython のセル(Python 3.14.7+IPython 9.19.1)では、1行目に例外の名前が並び、矢印 ----> がエラーの行を指しました。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[1], line 2
      1 n = "3"
----> 2 for i in range(n):
      3     print(i)

TypeError: 'str' object cannot be interpreted as an integer

読み方は同じです。Jupyter・Colab の画面そのものでは試していません。

13. よくある質問(FAQ)

Q. input() で数字を入れたのに TypeError になるのはなぜですか?

input() は入力を文字列で返すためです。3 を入れても <class 'str'> でした。int(input()) で変換すると range() や足し算に使えます(2章・3章)。

Q. エラー文で検索しても同じ英語が見つかりません。

版で文が違うことがあります。unhashable type: 'list' は 3.14 から前に説明が付きます(10章)。can only assign an iterable は、今回試した3版(3.12.10・3.14.7・3.15.2)では must assign iterable to extended slice でした(7章)。python -V で版を確かめてください。

Q. TypeError と ValueError の違いは何ですか?

型が合わないのが TypeError、型は合っていても中身が使えないのが ValueError です。range("3") は TypeError、int("abc") は ValueError でした。他の例外はPython エラー辞典の逆引き表で引けます。

本記事は、生成AIを活用して下書きし、運営者が内容を確認・編集したうえで公開しています。AIの利用方針は免責事項をご覧ください。

14. まとめ

  • 最終行の英語で原因を引き、エラーの行の値を print(x, type(x)) で確かめる
  • input() の値は文字列。range()・計算・添字の前に int()
  • 'NoneType' と出たら、sort() の戻り値か return 忘れ

直ったら次はこちら。

📌
参考にした一次情報と検証環境(2026年11月24日実測)

TypeError/list.sort/What's New 3.14。実測は Windows 11 の Python 3.15.2・3.14.7・3.12.10 です。NumPy は 2.4.4(3.12.10)・2.5.5(3.14.7)、IPython は 9.19.1(3.14.7)です。