栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

你活了多久----快用Python计算一下日期

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

你活了多久----快用Python计算一下日期

输入你的出生日期和现在的日期或者死亡日期,程序会自动计算你活了多久

# 判断是否为闰年
def runYear(year):
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        return 1
    else:
        return 0


# 计算天数
def countDay(currentDay):
    perMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    totalDay = 0
    year = 1970     # 1970年1月1日时间戳,算是电脑出生的日子
    while year < currentDay['year']:
        if runYear(year):
            totalDay = totalDay + 366
        else:
            totalDay = totalDay + 365
        year += 1
    if runYear(currentDay['year']) == 1:
        perMonth[2] += 1
    i = 1
    while i < currentDay['month']:
        totalDay += perMonth[i]
        i += 1
    totalDay += currentDay['day']
    return totalDay


if __name__ == "__main__":
    try:
        print("请输入出生日期年,月,日(例如:2000 1 31):")
        year1, month1, day1 = map(int, input().split())  # 表示连续输入3个int型并分别保存给
        dateBirth = {'year': year1, 'month': month1, 'day': day1}
        print("请输入今天的日期年,月,日(例如:2021 11 30):")
        year2, month2, day2 = [int(i) for i in input().split()]
        today = {'year': year2, 'month': month2, 'day': day2}
        totalDay1 = countDay(dateBirth)
        totalDay2 = countDay(today)
        print("您从%d年%d月%d日出生到%d年%d月%d日:经历了%d天"
              % (year1, month1, day1, year2, month2, day2, totalDay2 - totalDay1))
    except:
        print("输入格式不对,重新运行程序")

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/648497.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号