类型错误:“函数”对象不可下标 - Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29101836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: 'function' object is not subscriptable - Python
提问by HorrorBoy Jay
I've tried to solve an assignment with this code:
我试图用这个代码解决一个任务:
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: ")))
But when I run it, I get the error:
但是当我运行它时,我收到错误:
TypeError: 'function' object is not subscriptable
I don't understand where this is coming from...
我不明白这是从哪里来的......
回答by unutbu
You have two objects both named bank_holiday
-- one a list and one a function. Disambiguate the two.
你有两个命名的对象bank_holiday
——一个是列表,一个是函数。消除两者的歧义。
bank_holiday[month]
is raising an error because Python thinks bank_holiday
refers to the function (the last object bound to the name bank_holiday
), whereas you probably intend it to mean the list.
bank_holiday[month]
正在引发错误,因为 Python 认为bank_holiday
是指函数(绑定到 name 的最后一个对象bank_holiday
),而您可能希望它表示列表。