typeerror'builtin_function_or_method'对象没有属性'__getitem__'

Rom*_*vis 17 python typeerror

这是代码:

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]
Run Code Online (Sandbox Code Playgroud)

这段代码应该是Pascal的三角形.错误发生在第六行.

Nat*_*usa 62

您需要将括号更改The_End.insert[x + 1, y]为括号.

The_End.insert(x + 1, y)
Run Code Online (Sandbox Code Playgroud)

在Python中使用小写变量名称是很好的做法.大写通常用于课程.


Ash*_*ary 17

您需要括号而不是[]:

The_End.insert(x + 1, y)
Run Code Online (Sandbox Code Playgroud)