Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local variable 'list' referenced before assignment

Tags:

python

I made a simple script that converts any input text into a "code" and can also translate it back. It only works one word at a time.

I want to make the script adds each new code to a list that is printed every time. For example, the first time you translate something, "HELLO" becomes "lohleci". The second time, I want it not only to show "world" = "ldwropx", but also state below everything translated so far.

I'm new to Python and have looked through forums for people with similar problems. The way I tried doing it (a segment was removed and put into a separate script), I get an error saying "local variable 'list' referenced before assignment." This is the code producing the error:

list = "none"
def list():
    word = raw_input("")
    if list == "none":
        list = word + " "
        print list
        list()
    else:
        new_list = list + word + " "
        list = new_list
        print list
        list()
list()
like image 992
James Hartman Avatar asked Sep 26 '14 04:09

James Hartman


1 Answers

Your code has several problems, all of which are fixable with a bit more knowledge.

  1. Don't use the name list for your own variables or functions. It's the name of a built-in Python function, and if you use that name for your own functions you won't be able to call the built-in function. (At least, not without resorting to advanced tricks which you shouldn't be trying to learn yet.)
  2. You're also re-using the same name (list) for two different things, a variable and a function. Don't do that; give them different, meaningful names which reflect what they are. E.g., wordlist for the variable that contains a list of words, and get_words() for your function.
  3. Instead of using a variable named list where you accumulate a set of strings, but which isn't actually a Python list, why not use a real Python list? They're designed for exactly what you want to do.

You use Python lists like this:

wordlist = []
# To add words at the end of the list:
wordlist.append("hello")
# To print the list in format ["word", "word 2", "word 3"]:
print wordlist
# To put a single space between each item of the list, then print it:
print " ".join(wordlist)
# To put a comma-and-space between each item of the list, then print it:
print ", ".join(wordlist)

Don't worry too much about the join() function, and why the separator (the string that goes between the list items) comes before the join(), just yet. That gets into classes, instances, and methods, which you'll learn later. For now, focus on using lists properly.

Also, if you use lists properly, you'll have no need for that if list == "none" check you're doing, because you can append() to an empty list just as well as to a list with contents. So your code would become:

Example A

wordlist = []

def translate_this(word):
    # Define this however you like
    return word

def get_words():
    word = raw_input("")
    translated_word = translate_this(word)
    wordlist.append(translated_word)
    print " ".join(wordlist)
    # Or: print ", ".join(wordlist)
    get_words()

get_words()

Now there's one more change I'd suggest making. Instead of calling your function at the end every time, use a while loop. The condition of the while loop can be anything you like; in particular, if you make the condition to be the Python value True, then the loop will never exit and keep on looping forever, like so:

Example B

wordlist = []

def translate_this(word):
    # Define this however you like
    return word

def get_words():
    while True:
        word = raw_input("")
        translated_word = translate_this(word)
        wordlist.append(translated_word)
        print " ".join(wordlist)
        # Or: print ", ".join(wordlist)

get_words()

Finally, if you want to get out of a loop (any loop, not just an infinite loop) early, you can use the break statement:

Example C

wordlist = []

def translate_this(word):
    # Define this however you like
    return word

def get_words():
    while True:
        word = raw_input("")
        if word == "quit":
            break
        translated_word = translate_this(word)
        wordlist.append(translated_word)
        print " ".join(wordlist)
        # Or: print ", ".join(wordlist)

get_words()

That should solve most of your problems so far. If you have any questions about how any of this code works, let me know.

like image 60
rmunn Avatar answered Sep 28 '22 08:09

rmunn