1. 程式人生 > >工作需要寫了個小指令碼,嗯...就醬

工作需要寫了個小指令碼,嗯...就醬

# -*- coding:utf-8 -*-
import random

x=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
d={
    '0':'1',
    '1':'0',
    '2':'X',
    '3':'9',
    '4':'8',
    '5':'7',
    '6':'6',
    '7':'5',
    '8':'4',
    '9':'3',
    '10':'2'}

def check(code):
    sum=0
    n=0
    for i in code :
        sum += int(i)*x[n]
        n+=1
    result=sum%11
    return d[str(result)]

def make_card():
    # 地區 150105
    area=150105
    # 年份 1940~1998
    year=random.randint(1940,1998)
    # 月份
    month=random.randint(1,12)
    # 日期
    day=random.randint(1,28)
    # 性別
    gender=random.randint(1,2)
    # 派出所 78
    s='%d%d%02d%02d78%d' % (area,year,month,day,gender)
    cardid=s+check(s)
    return cardid

print(make_card())

# 生成指定資料的身份證號
def make_card_file(n):
    with open('cards.txt','w',encoding='utf-8',newline='\r') as f:
        for i in range(n-1):
            f.write(make_card()+'\r')
        f.write(make_card())

make_card_file(10)