简体   繁体   English

修复围绕“分配前引用的本地变量”的UnboundLocalError问题。

[英]Fixing UnboundLocalError revolving around “local variable ' ' referenced before assignment.”

I am trying to execute a translation with a module, though keep getting this error message relating to some of my variables: 我试图通过模块执行翻译,尽管不断收到与我的某些变量有关的错误消息:

UnboundLocalError: local variable 'usedWord' referenced before assignment

Here's my code for the reference: 这是我的代码供参考:

def message_translate():
    for message in r.get_unread():
        message_text = message.body.lower()
        messageList = message_text.split()
        for word in message_text:
            if word in fullLangNames:
                usedWord = word
                index = fullLangNames.index(word)
                transLang = supportedLangs[index]
            elif word in abbrevLangNames:
                usedWord = word
                index = abbrevLangNames.index(word)
                transLang = supportedLangs[index]

        finalized_message_text = message_text.replace("/u/pleaseTranslate", "") and message_text.replace(usedWord, "")
        translation = (translator.translate(finalized_message_text, lang_from = 'en', lang_to = transLang))
        callback = (transMessage % translation)
        message.mark_as_read

Now I don't understand why I'm getting that error, or what the error is telling me. 现在,我不明白为什么会收到该错误,或​​者该错误告诉我什么。 I apologize if this is an easy fix, though I've looked around and haven't found anything of significant use. 我很抱歉,这是一个简单的解决方法,尽管我环顾四周,还没有发现任何有意义的用途。

Thanks and I appreciate the help! 谢谢,我感谢您的帮助!

It looks to me that the problem is in the finalized_message_text. 在我看来,问题出在finalized_message_text中。 You should initialize usedWord = "" before the for loop. 您应该在for循环之前初始化usedWord =“”。 That should fix it. 那应该解决它。 Or add the final Else statement to initialize usedWord = "" 或添加最终的Else语句以初始化usedWord =“”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 修复UnboundLocalError:在python中赋值之前引用的局部变量“名称” - Fixing UnboundLocalError: local variable 'name' referenced before assignment in python UnboundLocalError:赋值前引用的局部变量'x'。 在seaborn包中正确使用tsplot用于数据帧? - UnboundLocalError: local variable 'x' referenced before assignment. Proper use of tsplot in seaborn package for a dataframe? 在这段代码中,我得到 UnboundLocalError: local variable 'click' referenced before assignment。 我不知道为什么? - In this code i get UnboundLocalError: local variable 'click' referenced before assignment. I do not know why? Python 递归错误:UnboundLocalError:在赋值之前引用了局部变量“n”。 洗牌台 python 代码 - Python recursion error: UnboundLocalError: local variable 'n' referenced before assignment. Shuffling deck python code UnboundLocalError:分配前引用的局部变量“秘密”。 AWS 机密管理器 python 代码 - UnboundLocalError: local variable 'secret' referenced before assignment. AWS secrets manager python code “变量___在分配前已被引用。” - “Variable ___ referenced before assignment.” Python – UnboundLocalError:分配前引用了局部变量'p'。 While循环根据我输入的时间提供不同的输出 - Python – UnboundLocalError: local variable 'p' referenced before assignment. While Loop giving different outputs depending when I input UnboundLocalError:分配前已引用局部变量“ totalp” - UnboundLocalError: local variable 'totalp' referenced before assignment unboundLocalError:分配前已引用局部变量“ arm”? - unboundLocalError: local variable 'arm' referenced before assignment? UnboundLocalError:赋值前引用了局部变量“n” - UnboundLocalError: local variable 'n' referenced before assignment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM