Python Forum
Referenced before assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Referenced before assignment
#1
Hi All,

Im having an error where it says that the variable is being referenced before assignment. As seen below.

Error:
Incorrect Username, please try again Exception in Tkinter callback Traceback (most recent call last): File "D:\Work\School\ComputerScience\Python Files\lib\tkinter\__init__.py", line 1892, in __call__ return self.func(*args) File "D:\Work\School\ComputerScience\Code\Other\Tkinter Testing.py", line 40, in clicked if password == password_check: UnboundLocalError: local variable 'password_check' referenced before assignment
The code that is being run to create this issue is below.

def clicked():
        username=username_1.get()
        password=password_1.get()
            

        try:
            password_check = open("D:/Work/School/ComputerScience/Code/Other/TkinterTestingUserDetails/" + username + ".txt","r").read() #looking at what is in the file (the password)
        except:
            print ("Incorrect Username, please try again")
            username_1.delete(0, tk.END)
            password_1.delete(0, tk.END)

        if password == password_check:
            print ("Login Succesful")
            window_2.destroy()
            print ("Complete")
            #game()
        else:
            print("Login Unsuccesful, incorrect password, please try again")
            username_1.delete(0, tk.END)
            password_1.delete(0, tk.END)
            

    label_blank_2=tk.Label(text=" ")
    label_blank_2.grid(row=4)

    button=tk.Button(text="Click", fg="white", bg="blue", width=25, height=5, command=clicked)
    button.grid(row=5)

    window_2.mainloop()
If you could tell me what to fix that would be great!
Reply
#2
Your error is on line 40, this script hasno line 40, are you importing this code to another python file? Basically when it gets to line 40 to validate the password is correct, it is saying it has no value for passeord_check.
Reply
#3
Think about the flow of execution in that function: what happens if an exception bis thrown on line 7, so that execution enters the except block?
Reply
#4
(Mar-02-2021, 07:46 PM)damian0612 Wrote: Your error is on line 40, this script hasno line 40, are you importing this code to another python file? Basically when it gets to line 40 to validate the password is correct, it is saying it has no value for passeord_check.

I have only sent a section of the code. Line 40 is where it says:

  if password == password_check: 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  local varible referenced before assignment SC4R 6 1,525 Jan-10-2023, 10:58 PM
Last Post: snippsat
  UnboundLocalError: local variable 'wmi' referenced before assignment ilknurg 2 1,915 Feb-10-2022, 07:36 PM
Last Post: deanhystad
  ReferenceError: weakly-referenced object no longer exists MrBitPythoner 17 11,555 Dec-14-2020, 07:34 PM
Last Post: buran
  UnboundLocalError: local variable 'figure_perso' referenced before assignment mederic39 2 2,279 Jun-11-2020, 12:45 PM
Last Post: Yoriz
  local variable 'marks' referenced before assignment Calli 3 2,333 May-25-2020, 03:15 PM
Last Post: Calli
  UnboundLocalError: local variable referenced before assignment svr 1 3,311 Dec-27-2019, 09:08 AM
Last Post: perfringo
  UnboundLocalError: local variable ' ' referenced before assignment d3fi 10 5,571 Sep-03-2019, 07:22 PM
Last Post: buran
  local variable referenced before assignment. samvup 3 4,620 Aug-26-2019, 07:07 PM
Last Post: buran
  local variable 'option' referenced before assignment RedSkeleton007 11 7,963 Feb-23-2018, 07:16 AM
Last Post: RedSkeleton007
  UnboundLocalError: local variable 'Num' referenced kunalmetha 2 3,802 Apr-19-2017, 02:44 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020