python不使用日期函数,判断一个人从生日到现在过了多久。

5 篇文章 0 订阅
1 篇文章 0 订阅

今天做了个题,要求在不使用内置日期函数的情况下,算出你从出生到现在的天数。下面是思考路径。

首先,对使用isdigit函数,判断输入是否都是数字组成,然后通过screen_date函数,判断输入的日期是否符合条件。

if __name__ == "__main__":
    print(50 * "*")
    print("计算你从出生到现在活了多少天。")
    while True:
        year = input("你的生日是哪一年?")
        month = input("你的生日是哪个月?")
        day = input("你的生日是多少号?")
        now_year = input("请输入你现在的日期是哪一年?")  # 定义现在的年
        now_month = input("请输入你现在的日期是哪个月?")  # 定义现在的月
        now_day = input("请输入你现在的日期是哪一天?")  # 定义现在的天
        if year.isdigit() and month.isdigit() and day.isdigit() and \
                now_year.isdigit() and now_month.isdigit() and now_day.isdigit():  # 判断输入是否合理
            year = int(year)  # 初始化数据类型
            month = int(month)  # 初始化数据类型
            day = int(day)  # 初始化数据类型
            now_year = int(now_year)  # 初始化数据类型
            now_month = int(now_month)  # 初始化数据类型
            now_day = int(now_day)  # 初始化数据类型
            if screen_date(year, month, day) and screen_date(now_year, now_month, now_day):  # 通过判断函数,判断日期范围是否合理
def screen_date(year, month, day):  # 判断输入的值是否符合要求
    if leap_year(year):  # 如果是闰年
        if month == 2 and 0 < day <= 29:
            return True
        elif month in (1, 3, 5, 7, 8, 10, 12) and 0 < day <= 31:
            return True
        elif month in (4, 6, 9, 11) and 0 < day <= 30:
            return True
    else:  # 如果不是闰年
        if month == 2 and 0 < day <= 28:
            return True
        elif 0 < day <= 31 and month in (1, 3, 5, 7, 8, 10, 12):
            return True
        elif 0 < day <= 30 and month in (4, 6, 9, 11):
            return True
    return False

然后符合条件就执行主函数

 if screen_date(year, month, day) and screen_date(now_year, now_month, now_day):  # 通过判断函数,判断日期范围是否合理
                day_times = main(year, month, day, now_year, now_month, now_day)
                print("从%s年%s月%s日开始到%s年%s月%s日,已经活了%s天了!你真厉害!" %
                      (year, month, day, now_year, now_month, now_day, day_times))
                break
        print("输入格式不符合,请重新输入:")
        continue
    print(50 * "*")

使用leap_year函数来判断是否为闰年,能整除4且不能整除100或能整除400的为闰年。

def leap_year(year):  # 判断是否是闰年
    return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0  # 是闰年就返回True否则返回False

通过months_days函数来的出日期的天数,先将每个月份的天数存入列表,判断年份是否为闰年,如果是就将2月的天数变为29天

def months_days(year, month=12, day=0):  # 计算出当前年月日的天数
    day_times = 0  # 定义天数变量
    month_list = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]  # 将每个月的天数保存在列表
    if leap_year(year):  # 判断是闰年就将2月的天数变为29
        month_list[1] = 29
        for i in range(month - 1):  # 循环计算天数
            day_times += (month_list[i])
    else:
        for i in range(month - 1):  # 循环计算天数
            day_times += (month_list[i])
    return day_times + day  # 得出总天数

最后,通过循环得出两个日期相差的年份,来计算出每年的天数,相加得出总天数

def main(year, month, day, now_year, now_month, now_day):  # 主函数
    day_times = 0  # 定义总天数
    birth_day = months_days(year, month, day)  # 得出生日日期的天数
    now_days = months_days(now_year, now_month, now_day)  # 得出当前日期的天数
    if year == now_year:  # 如果生日日期等于当前日期则的出日期差
        day_times += now_days - birth_day
    else:  # 得出生日日期和当前日期中间的天数
        for i in range(year + 1, now_year):
            day_times += months_days(i)
            print(day_times)
        if leap_year(year):  # 如果生日日期是闰年就用366减去生日日期,得出生日日期的天数
            birth_day = 366 - birth_day
        else:  # 否则用365减去生日日期
            birth_day = 365 - birth_day
        day_times += birth_day + now_days  # 将生日日期天数和当前日期天数与中间的日期天数相加
    return day_times  # 返回最终的天数

不多说了,下面是完整代码。

```python
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Time    : 2020/4/22 9:52
Name    : 茅十八
File    : work_06.py
"""


def leap_year(year):  # 判断是否是闰年
    return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0  # 是闰年就返回True否则返回False


def months_days(year, month=12, day=0):  # 计算出当前年月日的天数
    day_times = 0  # 定义天数变量
    month_list = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]  # 将每个月的天数保存在列表
    if leap_year(year):  # 判断是闰年就将2月的天数变为29
        month_list[1] = 29
        for i in range(month - 1):  # 循环计算天数
            day_times += (month_list[i])
    else:
        for i in range(month - 1):  # 循环计算天数
            day_times += (month_list[i])
    return day_times + day  # 得出总天数


def screen_date(year, month, day):  # 判断输入的值是否符合要求
    if leap_year(year):  # 如果是闰年
        if month == 2 and 0 < day <= 29:
            return True
        elif month in (1, 3, 5, 7, 8, 10, 12) and 0 < day <= 31:
            return True
        elif month in (4, 6, 9, 11) and 0 < day <= 30:
            return True
    else:  # 如果不是闰年
        if month == 2 and 0 < day <= 28:
            return True
        elif 0 < day <= 31 and month in (1, 3, 5, 7, 8, 10, 12):
            return True
        elif 0 < day <= 30 and month in (4, 6, 9, 11):
            return True
    return False


def main(year, month, day, now_year, now_month, now_day):  # 主函数
    day_times = 0  # 定义总天数
    birth_day = months_days(year, month, day)  # 得出生日日期的天数
    now_days = months_days(now_year, now_month, now_day)  # 得出当前日期的天数
    if year == now_year:  # 如果生日日期等于当前日期则的出日期差
        day_times += now_days - birth_day
    else:  # 得出生日日期和当前日期中间的天数
        for i in range(year + 1, now_year):
            day_times += months_days(i)
            print(day_times)
        if leap_year(year):  # 如果生日日期是闰年就用366减去生日日期,得出生日日期的天数
            birth_day = 366 - birth_day
        else:  # 否则用365减去生日日期
            birth_day = 365 - birth_day
        day_times += birth_day + now_days  # 将生日日期天数和当前日期天数与中间的日期天数相加
    return day_times  # 返回最终的天数


if __name__ == "__main__":
    print(50 * "*")
    print("计算你从出生到现在活了多少天。")
    while True:
        year = input("你的生日是哪一年?")
        month = input("你的生日是哪个月?")
        day = input("你的生日是多少号?")
        now_year = input("请输入你现在的日期是哪一年?")  # 定义现在的年
        now_month = input("请输入你现在的日期是哪个月?")  # 定义现在的月
        now_day = input("请输入你现在的日期是哪一天?")  # 定义现在的天
        if year.isdigit() and month.isdigit() and day.isdigit() and \
                now_year.isdigit() and now_month.isdigit() and now_day.isdigit():  # 判断输入是否合理
            year = int(year)  # 初始化数据类型
            month = int(month)  # 初始化数据类型
            day = int(day)  # 初始化数据类型
            now_year = int(now_year)  # 初始化数据类型
            now_month = int(now_month)  # 初始化数据类型
            now_day = int(now_day)  # 初始化数据类型
            if screen_date(year, month, day) and screen_date(now_year, now_month, now_day):  # 通过判断函数,判断日期范围是否合理
                day_times = main(year, month, day, now_year, now_month, now_day)
                print("从%s年%s月%s日开始到%s年%s月%s日,已经活了%s天了!你真厉害!" %
                      (year, month, day, now_year, now_month, now_day, day_times))
                break
        print("输入格式不符合,请重新输入:")
        continue
    print(50 * "*")
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值