jilingxf

python由于括号问题 list.apend 报'builtin_function_or_method' object is not subscriptable 错误的一个坑

今天写几行代码解决工作问题,程序运行报报'builtin_function_or_method' object is not subscriptable  错误,

将代码简写如下

litterPigs = []
for boar in range(0,6):
    pig=[1,2,3,5 ]
    print(pig)
    try:
        litterPigs.append[pig]
    except BaseException as e:
        print(e)

运行结果为:

image

差了半天原因,最后发现是括号写错了,litterPigs.append[pig]中应该为litterPigs.append(pig)

改正后的代码和运行结果为

litterPigs = []
for boar in range(0,6):
    pig=[1,2,3,5 ]
    print(pig)
    try:
        litterPigs.append(pig)
    except BaseException as e:
        print(e)
print("***********************************")
print(litterPigs)

image

posted on 2023-02-22 21:42  jilingxf  阅读(173)  评论(0编辑  收藏  举报

导航