python输入出生年份、输出年龄_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()
總結
以上是生活随笔為你收集整理的python输入出生年份、输出年龄_python根据出生日期计算年龄的代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用FDTD软件仿真拓扑光子(六)-单向
- 下一篇: websocket python爬虫_p