1. ホーム
  2. python

[解決済み] typeerror 'builtin_function_or_method' オブジェクトに '__getitem__' という属性がない。

2022-01-31 02:07:24

質問

以下はそのコードです。

The_Start = [1,1]
The_End = [1, 1]
for z in range(20):
    for x in range(len(The_Start) - 1):
        y  = The_Start[x] + The_Start[x + 1]
        The_End.insert[x + 1, y]
    print The_End
    The_Start = The_End
    The_End = [1, 1]

このコードは、パスカルの三角形を作ることになっている。エラーは6行目にある。

解決方法は?

の括弧を変更する必要があります。 The_End.insert[x + 1, y] を括弧に変更します。

The_End.insert(x + 1, y)

Pythonでは、変数名には小文字を使うのがよい習慣です。大文字は一般的にクラスで使われます。