python制作日历_很难找到使用Python制作日历的方法

此代码展示了一个名为`show_formatted_calendar()`的函数,该函数接受5个参数,其中2个是必需参数,3个是可选参数,用于定制日历的显示。函数生成一个格式化的日历字符串,可用于打印、保存或重复使用。通过多次调用此函数并修改可选参数,可以改变日历的布局,包括单元格之间的空格数量、连接字符和角落字符。示例中提供了3次调用,每次调用都展示了不同的定制选项。
摘要由CSDN通过智能技术生成

@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}$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值