标签:ict return coding ptime int run lse get 就是
题目:
1.计算你的生日比如近30年来(1990-2019),每年的生日是星期几,统计一下星期几出现的次数比较多
2,生日提醒,距离生日还有几天
# !/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Hiuhung Wan
‘‘‘
1.计算你的生日比如近30年来(1990-2019),每年的生日是星期几,统计一下星期几出现的次数比较多
2,生日提醒,距离生日还有几天
‘‘‘
import datetime
def get_week_day(date):
week_day_dict = {0: ‘星期一‘, 1: ‘星期二‘, 2: ‘星期三‘, 3: ‘星期四‘, 4: ‘星期五‘, 5: ‘星期六‘, 6: ‘星期日‘}
return week_day_dict[date.weekday()]
def q1(str0):
year = int(str0.split(‘-‘)[0])
month = int(str0.split(‘-‘)[1])
day = int(str0.split(‘-‘)[2])
dict0 = {‘星期一‘: 0, ‘星期二‘: 0, ‘星期三‘: 0, ‘星期四‘: 0, ‘星期五‘: 0, ‘星期六‘: 0, ‘星期日‘: 0, } # 准备计数
for i in range(1990, 2020):
str_temp = str(i) + ‘-‘ + str(month) + ‘-‘ + str(day) # 转成字符串
dt_temp = datetime.datetime.strptime(str_temp, ‘%Y-%m-%d‘) # 转成date对象
dict0[get_week_day(dt_temp)] += 1 # 计数君
print(‘%s年的生日是:%s‘ % (i, get_week_day(dt_temp)))
list(dict0.values()).sort()
max_num = list(dict0.values())[-1] # 最大的数
list0 = list(dict0.keys())
list1 = []
for i in list0:
if dict0[i] == max_num:
list1.append(i)
print(‘%s出现的次数最多,达到了%s次。‘ % (list1, max_num))
print(‘-‘ * 50) # 分隔线
def q2(str0):
today = datetime.date.today()
current_year = str(today).split(‘-‘)[0] # 2019
cur_year_bir = current_year + str0[4:] # 今年生日
dt = datetime.datetime.strptime(cur_year_bir, ‘%Y-%m-%d‘) # 今年生日
m = dt.strftime(‘%j‘)
n = datetime.datetime.today().strftime(‘%j‘)
if int(m) > int(n): # 今年生日还没到
print(‘距今年生日还有%s天‘ % (int(m) - int(n)))
elif int(m) < int(n): # 今年生日已过,求到明年生日还有多少天
print(‘距明年生日还有%s天‘ % (365 - (int(n) - int(m)))) # 不考虑闰年
else:
print(‘今天就是生日‘)
def run():
#str0 = ‘1987-3-28‘
str0 = input(‘请输入你的生日,如:1950-1-1‘)
q1(str0)
q2(str0)
if __name__ == ‘__main__‘:
run()
标签:ict return coding ptime int run lse get 就是
原文地址:https://www.cnblogs.com/hiuhungwan/p/10624831.html