TypeError: 'builtin_function_or_method' object is not subscriptable的一种错误情况

本文深入探讨了Python初学者常遇到的TypeError: ‘builtin_function_or_method’ object is not subscriptable错误,通过一个具体示例解释了错误产生的原因,并提供了正确的代码修改方式,帮助读者避免此类错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TypeError: ‘builtin_function_or_method’ object is not subscriptable的一种错误情况

初学python,今天在做练习时为一个Error苦恼了很久
最终找到了解决方法

错误代码块如下

def get_new_nums():
	numbers = input('Well, I don\'t know your favorite numbers yet, '+
		'can you tell me?')
	numbers = numbers.split(',')	
	print(int(numbers[0]))
	numbers = [int(numbers[i]) for i in range(len[numbers])]
	with open('favorite_nums.json','w') as fav_nums:
		json.dump(numbers,fav_nums)

运行后返回:TypeError: ‘builtin_function_or_method’ object is not subscriptable

原因:我把倒数第三行列表解析里面的len(numbers)打成len[numbers]了,小括号打成了中括号
函数不能当字典用,因此返回 ‘builtin_function_or_method’ object is not subscriptable

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值