随机输入日期计算星期几(1900年1月1日为基础)

已知,1900年1月1日是星期1,用户随机输入年月日,计算星期几

第一版,纯python基础代码实现

  1 year=int(input('year')) 2 month=int(input('month'))

 3 day=int(input('day'))
 4 if year >=1900:#大于等于1900
 5     ryear =1900
 6     sum =0
 7     while ryear < year:#1901
 8         if ((ryear % 4 == 0) and (ryear %100 !=0))or(ryear % 400 == 0):#闰年
 9             sum+=2
10         else:
11             sum+=1
12         ryear+=1
13     #计算月份移动
14     mon = month
15     r_month = 1
16     while r_month < month:#计算到mon月份1号
17         if r_month in [1,3,5,7,8,10,12]:
18             sum +=3
19             sum = sum % 7
20         elif r_month in [4,6,9,11]:
21             sum +=2
22         else:
23             if (year % 4 == 0 and year %100 != 0) or (ryear % 400 == 0):
24                 sum +=1
25             else:
26                 sum +=0
27         r_month+=1
28     #计算天
29     sumday = day -1
30     sum+=sumday
31     sum = sum % 7
32     #根据移动进行判断
33     if sum ==1:
34         print('星期2')
35     elif sum ==2:
36         print('星期3')
37     elif sum ==3:
38         print('星期4')
39     elif sum ==4:
40         print('星期5')
41     elif sum ==5:
42         print('星期6')
43     elif sum ==6:
44         print('星期7')
45     else:
46         print('星期1')
47 else:#计算小于1990年
48     ye = year
49     year +=1
50     sum = 0
51     while year<1900:
52         if (year % 4 == 0 and year %100 !=0) or(year % 400 == 0):#闰年
53             sum+=2
54         else:
55             sum +=1
56         year+=1
57     #计算月份移动
58     mo=month
59     month =month+1
60     while month <=12:#计算到mon月份1号
61         if month in [1,3,5,7,8,10,12]:
62             sum +=3
63         elif month in [4,6,9,11]:
64             sum +=2
65         else:
66             if (year % 4 == 0 and year %100!=0) or(year % 400 == 0):
67                 sum +=1
68             else:
69                 sum +=0
70         month+=1
71     #计算天
72     if mo in [1,3,5,7,8,10,12]:
73         sum += 31-day+1
74     elif mo in [4,6,9,11]:
75         sum += 30-day+1
76     else:
77         if (year % 4 == 0 and year %100 !=0) or (year % 400 == 0):
78             sum += 29-day+1
79         else:
80             sum += 28-day+1
81     sum = sum % 7
82     #根据移动进行判断
83     if sum ==1:
84         print('星期7')
85     elif sum ==2:
86         print('星期6')
87     elif sum ==3:
88         print('星期5')
89     elif sum ==4:
90         print('星期4')
91     elif sum ==5:
92         print('星期3')
93     elif sum ==6:
94         print('星期2')
95     else:
96         print('星期1')
 
97 print('game over') 98 99 星期几

 

posted @ 2018-04-28 19:40  dmyHero  阅读(2184)  评论(0编辑  收藏  举报