TypeError: '<invalid type>' does not support indexing

TypeError: '<invalid type>' does not support indexing

我有一项任务是能够使用我自己的函数制作任何年份的简单 3x4 日历。当 运行 我得到一个 TypeError。

这是我的代码

import math as m
import time as t

#Functions
def calcExcelDate(year, month) :
    day = 1
    y2 = year - 1900   # The number of years since 1900 with the simple subtraction
    em = (14 - month) // 12 # An early-month correction factor that is either 0 or 1
    y3 = y2 - em # year with early-month correction using y3 = y2 - em
    m2 = month + 12*em  # month with early-month correction
    l = 1 + min(y3, 0) + y3//4 - y3//100 # number of leap years since 1900
    d1 = m.floor(-1.63 + (m2 - 1)*30.6) # number of days preceding the given month in a non-leap year
    d2 = day + y3 * 365 + 1 + d1   # final excel date
    excelDate = d2
    return excelDate

def calcWeekDay(excelDate) :
    """Produce an int from 0 - 6 to represent which day"""
    daysInWeek = (excelDate + 6) % 7
    return daysInWeek

def daysInMonth(year, month) :   
    numDays = calcExcelDate(year, month + 1) - calcExcelDate(year, month)
    return int(numDays)

def formatCalendar(monthDetails) :
    """Return a calandar layout for one month. """
    return

def listMonthDays(year) :
    monthList = ("" , 'JANUARY', 'FEBUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVERMBER', 'DECENBER')

    MonthDetails = [year]
    for month in range(1, 13):
        blanks = ["",]
        MonthDetails.append[monthList[month], blanks*(int(calcWeekDay(calcExcelDate(year, month)))) + range(1, daysInMonth(year, month) + 1)]  
        # problem occures here ^
    return MonthDetails

def theEnd() :
    print "Programed by Gabriel Tsang"
    print "Date: " + t.ctime()
    print "End of processing"
    return

month = 0
year = 2015                             #year to make calander
monthDetails = listMonthDays(year)      #details for each month
print    # not yet finished still only working on the above

这是回溯

TypeError                                 Traceback (most recent call last)
c:\users\gabriel\appdata\local\temp\tmpdtfhw5.py in <module>()
     57 
     58 for month in range(1, 13, 3) :
---> 59     left = formatCalendar(listMonthDays(year)[month]).split('\n')
     60     centre = formatCalendar(listMonthDays(year)[month+1]).split('\n')
     61     right = formatCalendar(listMonthDays(year)[month+2]).split('\n')

c:\users\gabriel\appdata\local\temp\tmpdtfhw5.py in listMonthDays(year)
     42     for month in range(1, 13):
     43         blanks = ["",]
---> 44         MonthDetails.append[monthList[month], blanks*(int(calcWeekDay(calcExcelDate(year, month)))) + range(1, daysInMonth(year, month) + 1)]
     45     return MonthDetails
     46 def theEnd() :

TypeError: 'builtin_function_or_method' object has no attribute '__getitem__' 

改变这个

MonthDetails.append[monthList[month], blanks*(int(calcWeekDay(calcExcelDate(year, month)))) + range(1, daysInMonth(year, month) + 1)]  

对此

MonthDetails.append(monthList[month], blanks*(int(calcWeekDay(calcExcelDate(year, month)))) + range(1, daysInMonth(year, month) + 1))

[] 符号用于索引(在底层调用 __getitem__ 方法)。通过键入 MonthDetails.append[],您试图索引内置函数 list.append。你想调用它,所以你应该使用 () 来代替。

如果您想向列表中添加多项内容,请使用 list.extend。它需要一个可迭代对象,然后将该可迭代对象中的每个项目添加到列表的末尾。

MonthDetails.extend([monthList[month], blanks*(int(calcWeekDay(calcExcelDate(year, month)))) + range(1, daysInMonth(year, month) + 1)])