用python根据用户输入的18位合法身份证号,输出用户的出生年月日、年龄和性别[第7、8、9、10位为出生年份(四位数);第11、第12位为出生月份;第13、14位代表出生日期;第17位代表性别,奇数为男,偶数为女
时间: 2023-05-11 12:02:31 浏览: 2292
以下是Python代码:
```python
import datetime
def get_info(id_num):
birth_year = id_num[6:10]
birth_month = id_num[10:12]
birth_day = id_num[12:14]
gender = int(id_num[16])
if gender % 2 == 0:
gender = '女'
else:
gender = '男'
birth_date = datetime.datetime.strptime(birth_year + '-' + birth_month + '-' + birth_day, '%Y-%m-%d')
age = (datetime.datetime.now() - birth_date).days // 365
return birth_date.strftime('%Y年%m月%d日'), age, gender
id_num = input('请输入18位合法身份证号:')
if len(id_num) != 18:
print('身份证号长度不正确')
else:
birth_date, age, gender = get_info(id_num)
print('出生日期:', birth_date)
print('年龄:', age)
print('性别:', gender)
```
如果您有其他编程问题,欢迎继续提问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)