python常见报错01 AttributeError: 'NoneType' object has no attribute 'append'
大多数是因为没注意到append方法是没有返回值的.问题出在这里:a = a.append(b)要知道,append方法是没有返回值的。也就是说,上述语句第一次会成功执行,并且将a赋值为None;第二次调用就会报错,因为None是不能调.append方法的,修改方法也简单,a.append(b)就可以了,不要接返回值例程如下:huase = ["红心", "黑桃", "方块...
·
大多数是因为没注意到append方法是没有返回值的.
问题出在这里:a = a.append(b) 要知道,append方法是没有返回值的。也就是说,上述语句第一次会成功执行,并且将a赋值为None;第二次调用就会报错,因为None是不能调.append方法的, 修改方法也简单,a.append(b)就可以了,不要接返回值
例程如下:
huase = ["红心", "黑桃", "方块", "梅花"]
shuzi = ["A", "2" , "3", "4", "5", "6", "7", "8", "9", "10", "j", "Q", "K"]
res = []
for e in huase:
for el in shuzi:
res = res.append((e, el)) #此处有错误,因为append没有返回值,
print(res)
解决方法
huase = ["红心", "黑桃", "方块", "梅花"]
shuzi = ["A", "2" , "3", "4", "5", "6", "7", "8", "9", "10", "j", "Q", "K"]
res = []
for e in huase:
for el in shuzi:
res.append((e, el)) #取消=
print(res)
更多推荐
已为社区贡献7条内容
所有评论(0)