TypeError:'function'对象不可订阅 - Python

Hor*_*Jay 8 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: ")))
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时,我收到错误:

TypeError: 'function' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)

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

unu*_*tbu 14

您有两个名为的对象bank_holiday- 一个是列表,另一个是函数.消除两者的歧义.

bank_holiday[month]引发错误是因为Python认为bank_holiday是指函数(绑定到名称的最后一个对象bank_holiday),而您可能打算将其表示为列表.