Local variable referenced before assignment using the try and except statement

I have some problems with try and except statements, I have an input widget that accepts input in strings, but I have code that converts it to an integer later, the problem is that the user types something like text which it gives out Error:

Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python3.2/tkinter/__init__.py", line 1402, in __call__ return self.func(*args) File "/home/ppppwn3d/workspace/Python/JailBreakBob/JailBreakBob.py", line 157, in buttonclick_gamescreen entryx = int(e1.get()) ValueError: invalid literal for int() with base 10: 'abc' 

So I wanted to hide the error with the try and except statements, but now I got another error message.

This is how it looks in code.

  while pressed == 8 : try: entryx = int(e1.get()) entryy = int(e2.get()) except ValueError: print("text") answerx = answerlistx[randomimage] answery = answerlisty[randomimage] if entryx == answerx and entryy == answery canvas.delete(images) randomimage = random.randrange(0,49+1) scorecounter = scorecounter + 1 game = PhotoImage(file=imagelist[randomimage]) images = canvas.create_image(30, 65, image = game, anchor = NW) e1.delete(0, END) e2.delete(0, END) pressed = '' if entryx > 10 or entryx < -10 or entryy > 10 or entryy < -10 : wrong = canvas.create_image(30, 65, image = outside, anchor = NW) e1.delete(0, END) e2.delete(0, END) pressed = '' else: wrong = canvas.create_image(30, 65, image = incorrect, anchor = NW) e1.delete(0, END) e2.delete(0, END) pressed = '' 

New error message:

 text Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python3.2/tkinter/__init__.py", line 1402, in __call__ return self.func(*args) File "/home/ppppwn3d/workspace/Python/JailBreakBob/JailBreakBob.py", line 165, in buttonclick_gamescreen if entryx == answerx and entryy == answery: UnboundLocalError: local variable 'entryx' referenced before assignment 

I can’t understand why this is happening and how to fix it, so any help would be appreciated.

Thanks in advance.

+6
source share
4 answers

If there is an exception in your try/except block, there would be no entryx and entryy variables defined in the scope.

You must either quit or exit the program, or assign entryx and entryy values ​​in the except block.

Alternatively, you can make a while loop until the user enters an integer, as suggested here .

+6
source

To add @alecxe to the answer , I would rather modify the program as follows:

  entryx, entryy = 0, 0 try: entryx = int(e1.get()) entryy = int(e2.get()) except ValueError: print("text") 

Basically, what the error means, in the case of an entryx, entryy error entryx, entryy never gets anything assigned, but still gets the link later in the if..else test.

It is best to have default values ​​for variables outside the try..except block.

+4
source

I would initialize answerx, answery with None before running the try block.

+1
source

you can see the text message.
therefore an exception was thrown and entryx was not initialized.
this is why you have:

 local variable 'entryx' referenced before assignment 
0
source

Source: https://habr.com/ru/post/951482/


All Articles