#!/usr/bin/env python
# coding:utf-8
import time  
wi = (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
ci = (1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2)
print '简介：该程序是一个可以将缺失的身份证补位的程序，不需联网。任意正确的二代18位身份证号码缺少一位都可以由本程序推算出来。使用方法：将所缺的一位用#号代替输入。例如：61092119900402388X，第一位缺失，即可输入#1092119900402388X。让程序自动为您计算。'
cnid = raw_input('缺失的身份证号码:')
pos = cnid.find('#')

def not_check_code():
  global wi
  global ci
  global cnid
  global pos
  if cnid[17] == 'X':
    re = 10
  else:
    re = int(cnid[17])
  re = ci[re]
  ru = re
  string = cnid.replace('#','0')
  re = 0
  for i in range(17):
    re = re + int(string[i]) * wi[i]
  b = 0
  while ((re + wi[int(pos)] * b) % 11) <> ru:
    b = b + 1
  return b
  
def check_code():
  global wi
  global ci
  global cnid
  re = 0
  for i in range(17):
    re = re + float(cnid[i]) * wi[i]
  re = re % 11
  re = ci[int(re)]
  if re == 10:
    re = 'X'
  return re

if pos == -1:
  print '你没有指出身份证缺少的位置！程序将在10秒内退出！'
  time.sleep(10)
  exit()
if len(cnid) <> 18:
  print '身份证号码输入有误！程序将在10秒内退出！'
  time.sleep(10)
  exit()
if pos == 17:
  returnnumber = check_code()
else:
  returnnumber = not_check_code()
  
print '缺失的位置:' + str((int(pos) + 1)) + ' 丢失的数字:' + str(returnnumber)
print '程序将在30秒内退出！'
time.sleep(30)

