日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Python - 按天算年龄

發布時間:2025/3/15 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python - 按天算年龄 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題:輸入出生日期和當前的日期,輸出活了多少天

舉例:你是昨天出生的,那么輸出就為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 = Falseif year % 400 == 0:result = Trueif year % 4 == 0 and year % 100 != 0:result = Truereturn resultdef 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 - day1if year1 == year2:days1 = 0i = 0while i < month1 - 1:days1 = days1 + daysOfMonths[i]i = i + 1days1 = days1 + day1if is_leap(year1) and month1 > 2:days1 = days1 + 1days2 = 0i = 0while i < month2 - 1:days2 = days2 + daysOfMonths[i]i = i + 1days2 = days2 + day2if is_leap(year2) and month2 > 2:days2 = days2 + 1days = days2 - days1else:days1 = 0i = 0while i < month1 - 1:days1 = days1 + daysOfMonths[i]i = i + 1days1 = days1 + day1if is_leap(year1) and month1 > 2:days1 = days1 + 1days2 = 0i = 0while i < month2 - 1:days2 = days2 + daysOfMonths[i]i = i + 1days2 = days2 + day2if is_leap(year2) and month2 > 2:days2 = days2 + 1days = 365 - days1 + days2if is_leap(year1):days = days + 1year1 = year1 + 1while year1 < year2:days = days + 365year1 = year1 + 1if is_leap(year1):days = days + 1return days# Test routinedef 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

?

轉載于:https://www.cnblogs.com/qingkai/p/8667501.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Python - 按天算年龄的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。