我不知道爲什麼通過創建兩個不同的實例我得到相同的結果?我的邏輯有什麼問題? (更新工作)兩個不同的實例給出相同的結果(Python)
基本上,這個類應該在根據一年
代碼本身創建日期列表:
class Date:
def __init__(self, year):
self.year = year
def dateStr(self, date):
if date < 10:
date = '0' + str(date)
else:
date = str(date)
return date
def daysInMonth(self, month):
if month == 4 or month == 6 or month == 9 or month == 11:
endDate = 30
if month == 1 or month == 3 or month ==5 or month == 7 or month ==8 or month == 10 or month == 12:
endDate = 31
if self.year%4 == 0 and month == 2:
endDate = 29
if self.year%4 != 0 and month == 2:
endDate = 28
return endDate
def makeDate(self):
self.date = []
month = 1
while month <= 12:
day = 1
while day <= self.daysInMonth(month):
self.date.append(str(self.dateStr(day)) + u'.' + str(self.dateStr(month)) + u'.' + str(self.year))
day += 1
month += 1
return self.date
def __str__(self):
return str(self.makeDate())
def __len__(self):
return len(self.makeDate())
def __getitem__(self, key):
return self.makeDate()[key]
date1 = Date(2012)
date2 = Date(2013)
print date1[364]
print date2[364]
感謝支持,
亞歷
你會得到什麼結果? –
'len(date)<366)'看起來不正確。您可能必須在'makeDate()''date''中使用'全球日期' – karthikr
2012年的兩個日期列表,而我預計2012年會有一個日期,2013年會有另一個。 – Alekz112