我有一项任务,可以使用我自己的功能为任何一年制作一个简单的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__'
答案 0 :(得分:0)
更改此
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
。它需要一个iterable,然后将该iterable中的每个项目添加到列表的末尾。
MonthDetails.extend([monthList[month], blanks*(int(calcWeekDay(calcExcelDate(year, month)))) + range(1, daysInMonth(year, month) + 1)])