TypeError: 'function' object is not subscriptable - Python

Related searches

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...

You have two objects both named bank_holiday -- one a list and one a function. Disambiguate the two.

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.

TypeError() constructor, The TypeError() constructor creates a new error when an operation could not be performed, typically (but not exclusively) when a value is not of´┐Ż There are three scenarios where a TypeError may be thrown. The first is where the argument type being passed to a function does not match its corresponding declared parameter type. The second is where a value being returned from a function does not match the declared function return type.

It is so simple, you have 2 objects with the same name and when you say: bank_holiday[month] python thinks you wanna run your function and got ERROR.

Just rename your array to bank_holidays <--- add a 's' at the end! like this:

bank_holidays= [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):
   if month <1 or month > 12:
       print("Error: Out of range")
       return
   print(bank_holidays[month-1],"holiday(s) in this month ­čśŹ")

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

Built-in Exceptions — Python 3.8.5 documentation, (When an object does not support attribute references or attribute assignments at all, TypeError is raised.) exception EOFError ´┐Ż. Raised when the input() function´┐Ż TypeError: "x" is not a constructor; TypeError: "x" is not a function; TypeError: "x" is not a non-null object; TypeError: "x" is read-only; TypeError: 'x' is not iterable; TypeError: More arguments needed; TypeError: Reduce of empty array with no initial value; TypeError: X.prototype.y called on incompatible type; TypeError: can't access dead

You can use this:

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

TypeError - Manual, The third is where an invalid number of arguments are passed to a built-in PHP function (strict mode only). Class synopsis ´┐Ż. TypeError extends Error {. /* Inherited´┐Ż Working with JavaScript Promise comes with its own array of errors, and a popular one is TypeError: Cannot read property 'then' of undefined. In this guide, we will cover two code examples containing a bugs that cause this TypeError and then refactor the code to resolve the issue. Example 1

What is an uncaught TypeError in JavaScript?, According to the Mozilla website for developer documents, “the TypeError object represents an error when a value is not of the expected type.” Uncaught means´┐Ż Filters: Retrieving Data from Server Retrieving Data from Server Top Level TypeError - AS3 : Properties | Properties | Constructor | Methods | Global Constants

TypeError: JavaScript, Per MDN: 'A TypeError is thrown when an operand or argument passed to a function is incompatible with the type expected by that operator or´┐Ż I'm trying to make an asynchronous web scraper using beautifulsoup and aiohttp.This is my initial code to start things.I'm getting a [TypeError: An asyncio.Future, a coroutine or an awaitable is required] and having a hard time figuring out what is wrong with my code.I am new to python and would appreciate any help regarding this.

JavaScript Error Handling, A close look at the TypeError: null or undefined has no properties TypeError within JavaScript, including a quick look at null and undefined´┐Ż Dismiss Grow your team on GitHub. GitHub is home to over 50 million developers working together. Join them to grow your own development teams, manage permissions, and collaborate on projects.

Comments
  • So....... you have a question?
  • I need to know what's wrong with my code.
  • It is so simple, you have 2 objects with the same name and when you say: bank_holiday[month] python thinks you wanna run your function and got ERROR. just rename your array to bank_holidays <--- add a 's' at the end!
  • It may also happen that you pass in a function reference as the parameter at the function call if you named them similar to a variable. That's what happened to me. I tried to index the parameter, which then was a function.
  • it will also happen when you miss paranthesis when calling the function..example in my case below. def maxProfit2(prices): return ([tomorrow - today for today, tomorrow in zip(prices, prices[1:]) if tomorrow - today > 0]) #calling print(maxProfit2[7,1,5,3,6,4])
  • Can you include inline code span so the code is readable & verifiable.