用python输入身份证然后显示其生日性别男女
时间: 2023-05-24 17:01:14 浏览: 71
以下是用 Python 输入身份证然后显示其生日、性别(男/女)的代码:
```
id_num = input("请输入您的身份证号码:")
# 生日
birth_year = id_num[6:10]
birth_month = id_num[10:12]
birth_day = id_num[12:14]
print(f"生日:{birth_year}年{birth_month}月{birth_day}日")
# 性别
gender = int(id_num[16])
if gender % 2 == 0:
print("性别:女")
else:
print("性别:男")
```
请注意,身份证号码的格式需要符合国家标准。如果输入的身份证号码不符合要求,可能会导致程序无法正确运行。
相关问题
python输入身份证得生日
要使用Python从身份证号码获取生日,可以通过正则表达式匹配身份证号码,然后提取出生年月日信息。下面是一个示例代码:
```python
import re
regx = '^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$'
id = input('输入身份证号码:')
while True:
id_result = re.search(regx, id)
if id_result is None:
print('身份证号码不合法!')
id = input('输入身份证号码:')
else:
break
id_no = id_result.group()
year = id_no[6:10]
month = id_no[10:12]
day = id_no[12:14]
print('生日:{}年{}月{}日'.format(year, month, day))
```
请注意,此代码使用正则表达式对身份证号码进行验证,确保其合法性。然后,使用字符串切片操作提取出生年月日信息,并将其打印出来。
python输入身份证号码判断性别
您可以使用身份证号码的第17位数字,如果是奇数则为男性,偶数则为女性。以下是python代码示例:
id_number = input("请输入身份证号码:")
if int(id_number[16])%2 == 0:
print("性别为女性")
else:
print("性别为男性")
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)