@Blooper,我编写了下面的代码,其中函数show_formatted_calendar()接受5个参数。在
前2个是必需参数,后3个是可选参数,如果要更改表的外观,可以传递这些参数。在
在代码中,我调用了show\u formatted\u calendar()3次。在show_formatted_calendar() returns a formatted calendar string that you can print on console, save in file or store in variable for reuse.
如果您运行它,它将要求您输入前2个必需参数的值,并使用最后3个参数的默认值(在第一次调用中)。在
在第2次和第3次调用中,它对前2个参数进行静态调用,并重写最后3个函数参数的默认值,以更改在底部提供的输出中可以看到的表的外观。在Customizing tables using optional parameters
spaces» Number of spaces before and after cell values. Default is 1.
fill_char» Charater to be used to join the 2 joints/corners. Default is -.
corner_char» A character used at joints or corners of calender table. default is +.
请看下面的代码。在def show_formatted_calendar(no_of_days, sun_starts_from, spaces=1, fill_char='-', corner_char='+'):
days = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'];
blank_fields = 0; # Blank fields in first row(may vary in range [1, 6])
if not(no_of_days >= 28 and no_of_days <= 31):
print 'Input Error: Number of days should be in interval [28, 31].';
return;
if not(sun_starts_from >= 1 and sun_starts_from <= 7):
print 'Input Error: Sunday should be inin interval [1, 7].'
return;
string = fill_char*spaces; # -
decorator_line = string + 3 * fill_char + string; # -
separator_line = (corner_char + decorator_line) * 7 + corner_char; # + -+ -+ -+ -+ -+ -+ -+
# First line
formatted_calendar = separator_line + '\n';
# Second line
line_spaces = ' ' * spaces;
days_string = "|" + line_spaces + (line_spaces + '|' + line_spaces).join(days) + line_spaces + '|';
formatted_calendar += days_string + '\n';
# Third line
formatted_calendar += separator_line + '\n';
# Fourth line (No of possible blank fields at the begining)
blank_fields = (8 - sun_starts_from) % 7; # 1=>0, 2=>6, 3=>5, 4=>4, 5=>5, 6=>2
blank_string = (('|' + line_spaces) + (3 * ' ') + line_spaces) * blank_fields;
date_string = '';
i = blank_fields + 1;
day = 1;
while day <= no_of_days:
date_string += '|' + line_spaces + '%-3s' % (day) + line_spaces;
if i % 7 == 0:
date_string += '|\n';
i += 1;
day += 1;
# No of possible blank fields in last line
last_blank_fields = 7 - ((no_of_days - (7 - blank_fields)) % 7);
last_blank_string = ('|' + line_spaces + 3 * ' ' + line_spaces) * last_blank_fields + '|';
formatted_calendar += (blank_string + date_string) + last_blank_string + '\n';
formatted_calendar += separator_line + '\n';
return formatted_calendar;
# Starts here
if __name__ == "__main__":
try:
no_of_days = int(raw_input('Enter number of days of month(>=28 & <=31) : ').strip());
sun_starts_from = int(raw_input('Sunday starts from which date(>=1 & <=7) : ').strip());
# First call
formatted_calendar = show_formatted_calendar(no_of_days, sun_starts_from);
print formatted_calendar;
# Second call (static input)
print "\nFor Days 31 days where sunday starts from 4:-\n"
formatted_calendar = show_formatted_calendar(31, 4, 2, '*', '.');
print formatted_calendar;
# Third call (static input)
print "\nFor Days 29 days where sunday starts from 2:-\n"
formatted_calendar = show_formatted_calendar(29, 2, 3, '~');
print formatted_calendar;
except Exception as error:
print 'Error occurred. ', error;
输出»
^{pr2}$