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

Python local variable 'charcheck' referenced before assignment, why?

我正在尝试使用 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() 

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

使用 global 调用访问全局变量

def submit(): 
    global numcheck, charcheck
    ......

您应该将 charchecknumcheck 移动到您的函数中:

def submit():
    charcheck = 0
    numcheck = 0

您的函数 def submit 不知道 charcheck 变量。你必须对你的函数说它是全局的:

def submit():
    global charcheck
    password=passw_var.get()    
    passw_var.set("")
    for i in range(len(password)):

如此有效,添加 global charcheck

您也可以这样做:

def submit(numbcheck,charcheck)



sub_btn=tk.Button(...,command = lambda:submit(numcheck,charcheck))

您的函数正在其局部范围内(在其缩进内)查找变量名。如果您不明白,请查阅 Python 变量作用域教程。您可以通过两种方式解决问题;将变量作为参数传递或使用 global 关键字,例如在下面的代码中:

def submit(): 
    global charcheck, numcheck, passw_var

    special_chars = "!@#$%^&*()_+[]\;',./{}|:\"<>?`~"
    password=passw_var.get()    
    passw_var.set("")
    for i in range(len(password)):
        if(password[i]) in special_chars:
            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)

我还编辑了条件以检查特殊字符,这说明了所有情况并且更具可读性。