我正在尝试编写一个程序来打印出一个日历,该日历将根据它们落后的日期打印出数字。我几乎已经解决了,但我不知道如何在周日之后让数字突破到一个新线。现在代码看起来像这样:
class Month:
def __init__(self, nDays, day1):
self.nDays = nDays
self.day1 = day1
def displayCalendar(self):
week = ' S M T W T F S '
print week
days = 1
if self.day1 == 2:
print ' ',
elif self.day1 == 3:
print ' ',
elif self.day1 == 4:
print ' ',
elif self.day1 == 5:
print ' ',
elif self.day1 == 6:
print ' ',
elif self.day1 == 7:
print ' ',
for i in range(self.nDays):
print '', days,
days = days + 1
print ' APRIL 2014'
april2014 = Month(30,3)
april2014.displayCalendar()
现在,我可以在适当的一天开始这个月,但数字只是继续在屏幕的边缘,不会再回到周一。我知道这可能是一些愚蠢的小细节,但我无法想象我的生活。
答案 0 :(得分:0)
以下代码将产生所需的结果(注意:它使用字符串格式化):
class Month:
def __init__(self, nDays, day1):
self.nDays = nDays
self.day1 = day1
def displayCalendar(self):
week = ' S M T W T F S '
print week
days = 1
if self.day1 == 2:
print ' ',
elif self.day1 == 3:
print ' ',
elif self.day1 == 4:
print ' ',
elif self.day1 == 5:
print ' ',
elif self.day1 == 6:
print ' ',
elif self.day1 == 7:
print ' ',
for i in range(self.nDays):
print '{:2}'.format(days),
if (self.day1 + i) % 7 == 0:
print ''
days = days + 1
print ' APRIL 2014'
april2014 = Month(30,3)
april2014.displayCalendar()
[OUTPUT]
APRIL 2014
S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30