用python实现计算黑色星期五

在西方,星期五和数字13都代表着坏运气,两个不幸的个体最后结合成超级不幸的一天。所以,如果某一天是13日又恰逢星期五,就叫“黑色星期五”。要求编写程序,输入某年年号和该年的元旦是星期几(为1-7的数字),输出该年所有的“黑色星期五”的日期(年/月/日)。给定如下约束条件:
①1900年1月1日是星期一。
②1月、3月、5月、7月、8月、10月和12月是31天。
③4月、6月、9月和11月是30天。
④2月是28天,在闰年是29天。
⑤年号能被4整除且又不能被100整除是闰年。
⑥能直接被400整除也是闰年。
输入:4位年号和该年元旦是星期几。
输出:所有的“黑色星期五”的日期(年/月/日)。

#!usr/bin/env python   
#-*- coding=utf-8 -*-   
from datetime import *
import calendar

class BlackFriday(object):
    def __init__(self,year):
        self.year=year
        
    def display(self):
        print 'The black Fridays of year %s is(%s-1-1 is %s):' % (self.year,self.year,datetime(self.year,1,1).strftime('%A'))
        L=[]
        for i in range(12):
            if 4==calendar.weekday(self.year,i+1,13):
                L.append(str(self.year)+'/'+str(i+1)+'/13')
               # print '%s' % datetime(self.year,i+1,13).strftime('%A')
        if None==L:
            print "There's none of Black Friday in year %s" % self.year
        else:
            print L
            
year=raw_input('请输入年份(1970~9999):')
BlackFriday(int(year)).display() 


保存为blackfriday.py后运行结果如下:
[root@localhost zhidao]# python blackfriday.py 
请输入年份(1970~9999):1970
The black Fridays of year 1970 is(1970-1-1 is Thursday):
['1970/2/13', '1970/3/13', '1970/11/13']
[root@localhost zhidao]# python blackfriday.py 
请输入年份(1970~9999):1986
The black Fridays of year 1986 is(1986-1-1 is Wednesday):
['1986/6/13']
[root@localhost zhidao]#

温馨提示:答案为网友推荐,仅供参考
相似回答