TypeError:’function’对象不可订阅 – Python
发布时间:2021-02-21 13:33:40 所属栏目:Python 来源:互联网
导读:我试图用这段代码解决一项任务: bank_holiday= [1, 0, 1, 1, 2, 0, 0, 1, 0, 0, 0, 2] #gives the list of bank holidays in each monthdef bank_holiday(month): month -= 1#Takes away the numbers from the months, as mon
我试图用这段代码解决一项任务: bank_holiday= [1,1,2,2] #gives the list of bank holidays in each month def bank_holiday(month): month -= 1#Takes away the numbers from the months,as months start at 1 (January) not at 0. There is no 0 month. print(bank_holiday[month]) bank_holiday(int(input("Which month would you like to check out: "))) 但是当我运行它时,我收到错误: TypeError: 'function' object is not subscriptable 我不明白这是从哪里来的…… 解决方法你有两个名为bank_holiday的对象 – 一个是列表,另一个是函数.消除两者的歧义.bank_holiday [month]引发错误,因为Python认为bank_holiday引用了函数(绑定到名称bank_holiday的最后一个对象),而您可能打算将其表示为列表. (编辑:十堰站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Python Django:在视图中,最好是为对象添加属性还是创建数据
- python – 是否可以使用__rmod__覆盖str的%行为?
- python – 更改Jupyter笔记本版本4.x徽标
- 格式化django中的电话号码
- python – Django Crispy Forms添加Div提交按钮
- python – 如何在DataFrame中增加groupby中的行数
- django – formfield_for_foreignkey和内联管理员
- 是否可以在Python中模拟Scala的特征?
- python – 用ElementTree写入带有utf-8数据的xml utf-8文件
- python – Fedora上的psycopg2