全局变量在 python 中除此以外的其他条件下工作

Global variable is working on other conditions except this one in python

大家好。我的 python 程序需要帮助。我几乎完成了一个警察档案系统的制作,您可以在其中输入和搜索。我的问题出在搜索功能上。这是开始使用的源代码。

    global Value
    def config1():
            Entry1.config(state="normal")
            Entry1.delete(first=0,last=99)
            Entry1.insert(0,"LastName")
            Entry1.config(state="disabled")
            ThisEntry.delete(first=0,last=99)
            Value = Police['text']

    def config5():
            Entry1.config(state="normal")
            Entry1.delete(first=0,last=99)
            Entry1.insert(0,"LastName")
            Entry1.config(state="disabled")
            ThisEntry.delete(first=0,last=99)
            Value = Visitor['text']

    def Search():
            Entry1.config(state="normal")
            x = Entry1.get()
            y = ThisEntry.get()
            Entry1.config(state="disabled")
            global Value

            if x == "LastName" and Value == "POLICE IDENTITY":
                    z = 0
                    ThisText.config(state="normal")
                    ThisText.delete("1.0","end")
                    c.execute("SELECT LastName FROM Police WHERELastName=?",(y,))
                    row = c.fetchall()
                    for a in row:
                            c.execute("SELECT * FROM Police WHERE LastName=?",(y,))
                            row = c.fetchall()

            elif x == "LastName" and Value == "VISITOR":
                    z = 0
                    ThisText.config(state="normal")
                    ThisText.delete("1.0","end")
                    ThisText.insert(INSERT,"VISITORS")
                    c.execute("SELECT LastName FROM Visitors WHERE LastName=?",(y,))
                    row = c.fetchall()
                    for a in row:
                            ThisText.insert(INSERT,"\n\n")
                            ThisText.insert(INSERT,"Visitor Number: ")
                            ThisText.insert(INSERT,row[z][0])
                            ThisText.insert(INSERT,"\n")
                            ThisText.insert(INSERT,"First Name: ")
                            ThisText.insert(INSERT,row[z][1])
                            ThisText.insert(INSERT,"\n")
                            ThisText.insert(INSERT,"Middle: ")
                            ThisText.insert(INSERT,row[z][2])
                            ThisText.insert(INSERT,"\n")
                            ThisText.insert(INSERT,"Last Name: ")

我使用函数 def config1():def config5(): 只是为了改变 Value 的值。我将 Value 声明为 global 因为我需要它只是为了更改值。

主要目标是,如果值等于我要搜索的记录并且等于我要搜索的记录类型,则条件将 return 一行Value

所以如果我在 Police Identity 中搜索 LastName,它会 return 一行。与搜索访客相同。

如果我在“警察身份”选项卡中搜索姓氏,它 return 是正确的一行。但每当我尝试在“访客”选项卡中搜索姓氏时,都会出现 return 这个错误:

    Traceback (most recent call last):
       File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1550, in __call__
       return self.func(*args)
       File "C:\Users\User\Desktop\School Files (Don't Touch)\Project in SE\Project in SE.py", line 1420, in Search
       elif x == "LastName" and Value == "POLICE IDENTITY":
       NameError: name 'Value' is not defined

我也尝试在函数本身上声明全局变量,但它仍然不起作用。我真的不明白为什么它在一种情况下有效但在另一种情况下不起作用。我不知道 Python 是不是在玩弄我还是什么。顺便说一句,我认为这个问题没有任何重复。

我不知道为什么错误似乎是在第二个条件而不是第一个条件下触发的 - 我怀疑还有其他错误 - 但通常你需要在分配给的函数中声明全局变量Value。这意味着将 global Value 添加到您的 config1config5 函数中。

global Value 声明在 Search 函数中 不是 所必需的,因为默认情况下程序应该在其中搜索 Value在局部变量中找不到后的全局变量。

以下代码说明了为什么可能会在第二个条件而不是第一个条件上触发错误:

global x

def foo():
    something = False
    if something and x == 10:
        print "Yes"
    elif x == 50:
        print "No"

foo()

这会导致错误:

line 15, in foo elif x == 50:

NameError: global name 'x' is not defined

虽然 x 被声明为全局变量,但没有为它赋值。这不会在第一个条件 if something and x == 10 上导致错误,因为 something 为 False,因此永远不会达到与 x 相关的条件。只有当程序达到第二个条件时,它才无法避免尝试评估 x,然后引发错误。