0

Good day to everyone. I need help with my python program. I am almost done making a police profile system wherein you can input and search. The problem I have is in the search function. Here is the bit of the source code to get started.

    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: ")

I used the functions def config1(): and def config5(): just to change the value of the Value. I declared the Value as global since I need it just for changing the value.

The main goal is that the condition will return a row if the value is equal to the record that I want to search and if it is equal to the type of record that I want to search which is the Value.

So if I search for the LastName in Police Identity, it would return a row. Same with searching for Visitor.

If I search for the last name in the Police Identity Tab, it returns a row properly. But whenever I try to search for the last name in the Visitor Tab, it returns this error:

    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

I tried to declare the global variable on the function itself also but it still doesn't work. I really don't get why it works on one condition but doesn't work on the other condition. I don't know if Python is playing a game on me or something. Btw I don't think there are any duplicates for this question.

Abraham
  • 11
  • 4
  • 1
    Possible duplicate of [Using global variables in a function other than the one that created them](http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them) – torek Mar 19 '17 at 13:38
  • Can you atleast tell me what is the error in my program? :( – Abraham Mar 19 '17 at 13:56
  • The error here relates to a condition `elif x == "LastName" and Value == "POLICE IDENTITY":`, which isn't in the code sample you give. In general, the `global Value` declaration needs to be in the functions that *assign* to `Value` (`config1` and `config5`) rather than in the function that refers to `Value` (`Search`) – Stuart Mar 19 '17 at 13:57
  • Simply amazing. My program worked after this one. Kudos to you sir Stuart. Next time I'll try to include the complete sample code so I won't confuse anyone in my question. Thanks. – Abraham Mar 19 '17 at 14:02

1 Answers1

0

I don't know why the error seems to be triggered on the 2nd condition rather than the 1st - I suspect there is some other bug - but in general you need to declare the global variable in the functions that assign to Value. This means adding global Value to your config1 and config5 functions.

The global Value declaration is not required within the Search function, because the programme should, by default, search for Value among global variables after failing to find it among the local variables.

The following code illustrates why an error might be triggered on the 2nd condition instead of the 1st:

global x

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

foo()

This causes an error:

line 15, in foo elif x == 50:

NameError: global name 'x' is not defined

Although x was declared as a global, no value was assigned to it. This doesn't cause an error on the first condition if something and x == 10, because something is False so the condition relating to x is never reached. It is only when the programme reaches the 2nd condition that it can't avoid trying to evaluate x, and then raises an error.

Stuart
  • 9,597
  • 1
  • 21
  • 30