问题
如何编写一个用身份证号判断年龄和性别的程序?
方法
代码清单 1
import datetime
ID=str(input("输入身份证号码"))
birthday_year=ID[6:10]
birthday_month=ID[10:12]
birthday_day=ID[12:14]
print("{0}年{1}月{2}日".format(birthday_year,birthday_month,birthday_day))
Now_Time=datetime.datetime.today()
today=int(str(Now_Time.month)+str(Now_Time.day))
if Now_Time.day<10:
today=int(str(Now_Time.month)+'0'+str(Now_Time.day))
if today-int(birthday_month+birthday_day)>=0:
age=Now_Time.year-int(birthday_year)
else:
age=Now_Time.year-int(birthday_year)-1
print("年龄:",age)
if int(ID[16])%2==0:
print("性别:女")
else:
print("性别:男")
结语
在使用Python解决某个具体问题时,要遵从逻辑性和严密性,构思出大体的程序框架,巧妙运用对应的Python函数,特别注意各处的缩进,提高效率和质量。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有