【问题标题】:TypeError: '<invalid type>' does not support indexingTypeError: '<invalid type>' 不支持索引
【发布时间】:2015-02-14 18:47:50
【问题描述】:

我的任务是使用我自己的函数为任何一年制作一个简单的 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__' 

【问题讨论】:

    标签: python function indexing


    【解决方案1】:

    改变这个

    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)])
    

    【讨论】:

    • 感谢它删除了错误,但现在它指出 TypeError: append() 在第 44 行仅接受 2 个参数(给定 3 个)
    • 看起来您正在尝试附加两个内容,monthList[month]blanks*(int(calcWeekDay(calcExcelDate(year, month)))) + range(1, daysInMonth(year, month) + 1)。如果您想在列表中添加两件事,请使用扩展。我已经相应地更新了我的答案
    • 感谢您的解释。真的帮了我大忙。
    猜你喜欢
    • 2019-08-18
    • 2013-08-23
    • 2018-01-07
    • 2013-06-23
    • 2019-04-18
    • 2016-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多