TypeError: ‘NoneType‘ object is not subscriptable解决办法

TypeError: ‘NoneType‘ object is not subscriptable解决办法,第1张

1.错误原因

TypeError: ‘NoneType’ object is not subscriptable

空类型对象不可以使用下标

报错代码:

# 生成大小 I*J 的矩阵,默认零矩阵
def makeMatrix(I, J, fill=0.0):
    m = []
    for i in range(I):
        m.append([fill] * J)
def __init__(self, ni, nh, no):
        # 建立权重(矩阵)
        self.wi = makeMatrix(self.ni, self.nh)
        # 设为随机值
        for i in range(self.ni):
            for j in range(self.nh):
                self.wi[i][j] = rand(-0.2, 0.2) 

报错代码是self.wi[i][j] = rand(-0.2, 0.2) ,后来发现wi对象有问题,追踪到代码self.wi = makeMatrix(self.ni, self.nh),发现makeMatrix没有返回值。


修改代码为:

def makeMatrix(I, J, fill=0.0):
    m = []
    for i in range(I):
        m.append([fill] * J)
    return m

读者可以参考python报错TypeError: ‘NoneType‘ object is not subscriptable的解决方法

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/571550.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存