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)

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. You should initialize usedWord = "" before the for loop. That should fix it. Or add the final Else statement to initialize usedWord = ""

Need Your Help

Using String.Format to specify the decimal places at runtime

.net precision numeric string.format

I want to use String.Format to specify the number of decimal places to display where the number of decimal places may change at runtime.

How to add elements to an injected array without over-writing the other elements in Flex 4 with Parsley

arrays actionscript-3 flex flex4 parsley

I have an array that i am using to store a collection of objects. My application allows the user to add new objects to the array and edit the content of the objects. all the objects in the array ar...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.