1. 程式人生 > >python生成北京地區固定年齡的身份證

python生成北京地區固定年齡的身份證

import random
def id_card():
    """生成身份證"""
    #身份證前17位係數
    num1 = [7, 9, 10, 5,8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
    number = str(random.choice([110101,110102,
    110105,110106,110107,110108,110109,110111,110112,110113,110114,
                            110115,110116,110117,110228,110229]))+"19900307"+str(random.randint(100,999))

    #前17位乘係數相加和11的餘數
    last_dict = {0:1,1:0 ,2:"X",3:9, 4:8, 5:7, 6:6, 7:5,8:4,9:3,10:2}
    sum1 = 0
    for index in range(len(number)):
        sum1 += int(number[index])*num1[index]
    number+=str(last_dict[sum1%11])
    return number