TypeError:“function”对象不可订阅-Python

2024-05-23 22:34:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用以下代码解决一个任务:

bank_holiday= [1, 0, 1, 1, 2, 0, 0, 1, 0, 0, 0, 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

我不明白这是从哪里来的。。。


Tags: ofthe代码inisholidaysnotat
1条回答
网友
1楼 · 发布于 2024-05-23 22:34:10

您有两个名为bank_holiday的对象——一个是列表,一个是函数。消除二者的歧义。

bank_holiday[month]引发了一个错误,因为Python认为bank_holiday引用了函数(绑定到名称bank_holiday的最后一个对象),而您可能希望它表示列表。

相关问题 更多 >