python根據出生日期計算年齡的代碼
python根據出生日期計算年齡的代碼,運行后會提醒用戶輸出出生的年月日,然后輸出年齡,可以改寫為一個通用函數
from time import *a function to find your age
def age(): print "Enter Your Date of Birth" d=input("Day:") m=input("Month:") y=input("Year:")
#get the current time in tuple format a=gmtime() #difference in day dd=a[2]-d #difference in month dm=a[1]-m #difference in year dy=a[0]-y #checks if difference in day is negative if dd<0: dd=dd+30 dm=dm-1 #checks if difference in month is negative when difference in day is also negative if dm<0: dm=dm+12 dy=dy-1 #checks if difference in month is negative when difference in day is positive if dm<0: dm=dm+12 dy=dy-1 print "Your current age is %s Years %s Months & %s Days"%(dy,dm,dd)
age()</pre>
本文由用戶 pycct 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!