typeerror: function object is not subscriptable

Cause of typeerror: function object is not subscriptable

Python is a flexible language that can be used to construct everything from simple scripts to complex applications. The abundance of modules that may be downloaded from the Python Package Index (PyPI), a database of software for the Python programming language, is one of the benefits of Python. However, occasionally you could see a notice saying that a certain module is missing. We’ll demonstrate how to fix the Python problem “typeerror: function object is not subscriptable” in this blog post. One can get this error while running the following code snippet:

 

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: ")))

 

The error one can get look as follows:

 

Solution

The major issue in this code is that it has the same function and variable name that is creating ambiguity. To solve this issue one needs to change the name of either the variable or the function in the above code snippet. 

The problem with having the same name in any code is that it confuses the compiler to understand what the user means by that particular name. That word can either point to that function or the variable. This is the main solution to the above problem.

 

bankHoliday= [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(bankHoliday[month])

bank_holiday(int(input("Which month would you like to check out: ")))

 

 

Also Read:  typeerror: not all arguments converted during string formatting

 

 

Share this post

2 thoughts on “typeerror: function object is not subscriptable

Leave a Reply

Your email address will not be published. Required fields are marked *