python,求解

博客内容涉及一个身份证验证程序的Python脚本,作者遇到字符串索引越界的问题。在`for`循环中,当`i=1`时,程序尝试访问`weight`列表的元素导致错误。问题可能出在对输入身份证号的处理上。解决方案可能包括检查身份证号的长度和格式是否正确,以及遍历权重数组时确保不会超出字符串长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

照着网上做的身份证核验的小脚本

为什么在函数jia()里当i=1,函数执行到for i in range(0,len(id)):   count = count + int(id[i]) * weight[i]  就报string index out  of range,   这里的weight[1]=9 啊,求指点一下吗?
源程序如下:
import tkinter as tk
from tkinter import *


def jia():
    id = var.get()     #610328 1955 0000 251(17) 
    if len(id) != 17 or id.isdigit() == False:     #身份核验
        L2["text"] = '输入错误'
    
    i = 0
    count = 0
    weight = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]  #权重项
    checkcode = {'0':'1','1':'1','2':'x','3':'9','4':'8','5':'7','6':'6','7':'5','8':'5','9':'3','10':'2'}
    #print(len(id))
    for i in range(0,len(id)):          #len(id)=17
    
        count = count + int(id[i]) * weight[i]
        id = checkcode[str(count % 11)]

        print("----------------------------")
    if c == int(digit[18]):
        L2['text'] = id
    else:
        L2['text'] = id
        
    B2['state']= 'normal'

def clear():
    L2['text'] = "    "
    var.set("")
    B2['state'] = 'disabled'


w = Tk()
var = StringVar()
w.title('身份证验证程序')
w.geometry('900x500+100+100')

L1 = tk.Label(w,text = "请输入身份证号码",font = ("Arial",30))
T1 = tk.Entry(w,font=("黑体",30),textvariable = var)
B1 = Button(w,text = '输入完成',command = jia, font=("黑体",20))
B2 = Button(w,text = '清除数据',command = clear,font=("黑体",20),state = "disabled")
L2 = tk.Label(w,text = "   ",font=("Arial",30))


B1.grid(row = 3, column = 20)
B2.grid(row = 4, column = 20)
L1.grid(row = 0, column = 45)
T1.grid(row = 1, column = 45)
L2.grid(row = 6, column = 45)
w.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值