gpt4 book ai didi

赋值前引用了Python局部变量 'charcheck',为什么?

转载 作者:行者123 更新时间:2023-12-02 16:25:06 25 4
gpt4 key购买 nike

<分区>

我正在尝试使用 python tkinter 编写密码验证器应用程序。如果插入的密码包含至少 2 个数字、至少 2 个特殊字符且长度至少为 7,则密码被认为是强密码,否则为弱密码。

如果我输入一个弱密码,程序会运行,但如果我输入一个强密码,我会遇到这个错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
return self.func(*args)
File "/home/liana/projects/python/modification/modify.py", line 15, in submit
charcheck += 1
UnboundLocalError: local variable 'charcheck' referenced before assignment

我不知道为什么。

这是我的代码:

import tkinter as tk 

numcheck = 0
charcheck = 0

root=tk.Tk()
root.geometry("600x400")
passw_var=tk.StringVar()

def submit():
password=passw_var.get()
passw_var.set("")
for i in range(len(password)):
if(password[i]=='!' or password[i]=='@' or password[i]=='#' or password[i]=='$' or password[i]=='&' or password[i]=='%' or password[i]=='*'):
charcheck += 1

elif (ord(password[i])>=48 and ord(password[i])<=57):
numcheck += 1

if (len(password)>=7 and charcheck>=2 and numcheck>=2):
result_label = tk.Label(root, text='STRONG', font=('calibre',10, 'bold')).grid(row=3, column=2)

else:
result_label = tk.Label(root, text='WEAK', font=('calibre',10, 'bold')).grid(row=3, column=2)

passw_label = tk.Label(root, text = 'Enter Your password: ', font = ('calibre',10,'bold'))

passw_entry=tk.Entry(root, textvariable = passw_var, font = ('calibre',10,'normal'), show = '*')

sub_btn=tk.Button(root,text = 'Submit',
command = submit)

passw_label.grid(row=1,column=0)
passw_entry.grid(row=1,column=1)
sub_btn.grid(row=2,column=1)

root.mainloop()

我想知道我在哪里犯了错误。谢谢

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com