TypeError: 'function' object is not subscriptable - Python

后端 未结 3 1240
忘了有多久
忘了有多久 2020-12-06 05:41

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 ban         


        
3条回答
  •  粉色の甜心
    2020-12-06 06:30

    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 

提交回复
热议问题