在使用 try 和 except 语句赋值之前引用的局部变量 [英] local variable referenced before assignment with try and except statement

查看:50
本文介绍了在使用 try 和 except 语句赋值之前引用的局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 try 和 except 语句上遇到了一些问题,我有一个输入小部件,它接受字符串中的输入,但我有稍后将其转换为整数的代码,问题是如果用户输入类似文本的内容,它会抛出一个错误如下:

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'

所以我想用 try 和 except 语句隐藏错误,但我现在收到另一条错误消息.

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

这是代码中的样子.

    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 = ''

新的错误信息:

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.

推荐答案

要添加到 @alecxe 的答案,我会而是像这样修改程序:

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

基本上,错误的意思是,在发生错误的情况下,entryx, entryy 永远不会被分配任何东西,但稍后仍会在 if..else 中被引用检查.

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.

最好为 try..except 块之外的变量设置默认值.

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

这篇关于在使用 try 和 except 语句赋值之前引用的局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆