码迷,mamicode.com
首页 > 编程语言 > 详细

Python - 按天算年龄

时间:2018-03-29 10:23:47      阅读:367      评论:0      收藏:0      [点我收藏+]

标签:pass   python   body   bst   alc   gpo   rect   技术   isp   

问题:输入出生日期和当前的日期,输出活了多少天

举例:你是昨天出生的,那么输出就为1

分三种情况讨论:

1、年份和月份都相同

2、年份相同月份不同,先计算出生当天是当年的第几天,后计算当前为当年的第几天,相减

3、年份不同,还是先计算出生当天为当年的第几天,后计算当前为当年的第几天,做闰年判断,逐一相加

闰年为一下两种情况

1、能被400整除

2、能被4整除但不能被100整除

 

、、、、、、、、、、、、、、、

 

本题来自Udacity的计算机科学导论课程,用来做Python入门

Python语言兼具一般高级语言和脚本语言的特点,在官网下了一个东东,只会做脚本,函数现在只会一行一行往里敲,然后运行,无法调试,好像是需要找一个开发环境,有空弄

附代码

技术分享图片
# By Websten from forums
#
# Given your birthday and the current date, calculate your age in days. 
# Account for leap days. 
#
# Assume that the birthday and current date are correct dates (and no 
# time travel). 
#


def is_leap(year):
    result = False
    if year % 400 == 0:
        result = True
    if year % 4 == 0 and year % 100 != 0:
        result = True
    return result

def daysBetweenDates(year1, month1, day1, year2, month2, day2):
    daysOfMonths = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    if year1 == year2 and month1 == month2:
        days = day2 - day1
    if year1 == year2:
        days1 = 0
        i = 0
        while i < month1 - 1:
            days1 = days1 + daysOfMonths[i]
            i = i + 1
        days1 = days1 + day1
        if is_leap(year1) and month1 > 2:
            days1 = days1 + 1
        days2 = 0
        i = 0
        while i < month2 - 1:
            days2 = days2 + daysOfMonths[i]
            i = i + 1
        days2 = days2 + day2
        if is_leap(year2) and month2 > 2:
            days2 = days2 + 1
        days = days2 - days1
    else:
        days1 = 0
        i = 0
        while i < month1 - 1:
            days1 = days1 + daysOfMonths[i]
            i = i + 1
        days1 = days1 + day1
        if is_leap(year1) and month1 > 2:
            days1 = days1 + 1
        days2 = 0
        i = 0
        while i < month2 - 1:
            days2 = days2 + daysOfMonths[i]
            i = i + 1
        days2 = days2 + day2
        if is_leap(year2) and month2 > 2:
            days2 = days2 + 1
        days = 365 - days1 + days2
        if is_leap(year1):
            days = days + 1
        year1 = year1 + 1
        while year1 < year2:
            days = days + 365
            year1 = year1 + 1
            if is_leap(year1):
                days = days + 1
    return days
        
        
        


# Test routine

def test():
    test_cases = [((2012,1,1,2012,2,28), 58), 
                  ((2012,1,1,2012,3,1), 60),
                  ((2011,6,30,2012,6,30), 366),
                  ((2011,1,1,2012,8,8), 585 ),
                  ((1900,1,1,1999,12,31), 36523)]
    for (args, answer) in test_cases:
        result = daysBetweenDates(*args)
        if result != answer:
            print "Test with data:", args, "failed"
        else:
            print "Test case passed!"

test()
View Code

 

Python - 按天算年龄

标签:pass   python   body   bst   alc   gpo   rect   技术   isp   

原文地址:https://www.cnblogs.com/qingkai/p/8667501.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!