时间实战2019-03-29

2019-03-29  本文已影响0人  swagsmile

时间和日期的综合小练习
1.计算你的生日比如近30年来(1990-2019),每年的生日是星期几,统计一下星期几出现的次数比较多
2,生日提醒,距离生日还有几天

"""时间和日期的综合小练习
1.计算你的生日比如近30年来(1990-2019),每年的生日是星期几,统计一下星期几出现的次数比较多
2,生日提醒,距离生日还有几天

大家如果生日不愿意泄露,可以随机写一个比如1990-1-1"""
from datetime import datetime
from collections import Counter


def get_birthday_weekday(birthday_str):
    weekday = datetime.strptime(birthday_str, '%Y-%m-%d').weekday()
    weekdays = ["Mon", "Tue", "Wes", "Thur", "Fri", "Sat", "Sun"]
    return weekdays[weekday]


def interval_days(name, your_birthday):
    try:
        birthday = datetime.strptime(your_birthday, '%Y-%m-%d')
        delta_days = (birthday - datetime.today()).days
        if delta_days > 0:
            return f'距离{name}的生日还有 {delta_days} 天'
        elif delta_days < 0:
            return f'距离{name}的生日已过 {abs(delta_days)} 天'
        else:
            return f'今天是{name}的生日,祝你生日快乐!!'
    except ValueError as e:
        print("please input the birthday format as : 1991-1-1")

def statist_birthday():
    birthdays = [str(y) + "-9-28" for y in range(1995,2020)]
    res = [get_birthday_weekday(day) for day in birthdays]
    return Counter(res)


if __name__ == "__main__":
    print(statist_birthday())
    birthdays = {"我": "2019-9-28"}
    for name, birth in birthdays.items():
        print(f"{interval_days(name, birth)}")
上一篇下一篇

猜你喜欢

热点阅读