14

I'm having some issues with the try and except statements, I have an entry widget that takes input in strings but I have code which converts it to a integer later, problem is if the user inputs something like text it throws an error like this:

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 I now get another error message.

This is what it looks like in the 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 = ''

The 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 figure out why this is happening and how to fix it so any help would be appreciated.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
ThatsNotMyName
  • 572
  • 2
  • 8
  • 24

4 Answers4

13

To add to @alecxe's answer, I would rather modify the program like so:

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

Basically, what the error means is that in case of an error, entryx, entryy never get assigned anything, but still get referenced later in the if..else check.

It's best to have default values for the variables outside of the try..except block.

Community
  • 1
  • 1
KGo
  • 18,536
  • 11
  • 31
  • 47
10

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

You should either throw and error and exit the program or assign entryx and entryy default values in the except block.

Also, you can make a while loop until the user enters an integer like it was suggested here.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
2

I would initialise answerx, answery with None before try block starts.

Lukasz
  • 426
  • 2
  • 13
0

you can see 'text' message.
so, exception was raised and entryx wasn't initialized.
that's why you've got:

local variable 'entryx' referenced before assignment