【问题标题】:typeerror 'builtin_function_or_method' object has no attribute '__getitem__'typeerror 'builtin_function_or_method' 对象没有属性 '__getitem__'
【发布时间】:2012-10-16 01:15:31
【问题描述】:

代码如下:

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]

这段代码应该是一个帕斯卡三角形。错误在第六行。

【问题讨论】:

    标签: python typeerror


    【解决方案1】:

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

    The_End.insert(x + 1, y)
    

    在 Python 中使用小写变量名是一种很好的做法。大写一般用于类。

    【讨论】:

      【解决方案2】:

      你需要括号而不是[]:

      The_End.insert(x + 1, y)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-31
        • 1970-01-01
        • 1970-01-01
        • 2012-12-04
        • 2012-10-15
        • 2014-01-16
        • 2017-02-27
        • 2018-01-28
        相关资源
        最近更新 更多