"NameError: name '' is not defined" when using two functions

I'm using one function to create a username and another function to create a password. The second function is not running because it tells me that "NameError: name 'list_two' is not defined". Here is my code:

import random
import openpyxl

path = "E:\\Desktop\\"

wb = openpyxl.load_workbook('excel.xlsx')
sheet = wb.active

def name_generator():
    usernames = list()
    for username in range (0, 34):
        color = ["Red", "Green", "Blue", "White", "Black", "Yellow", "Purple", "Orange", "Pink"]
        animal = ["Cat", "Dog", "Snake", "Mouse", "Tiger", "Leopard", "Moose", "Wolf", "Bear"]
        number = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                  "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"]

        randomColor = random.randrange(0, len(color))
        randomAnimal = random.randrange(0, len(animal))
        randomNumber = random.randrange(0, len(number))

        username = "Username: " + color[randomColor] + animal[randomAnimal] + number[randomNumber]
        usernames.append(username)
    return usernames

list = name_generator()

for i in range(1,34):
    cellref = sheet.cell(row=i, column=5)
    cellref.value=list[i]

def password_generator():
    passwords = list_two()
    for password in range (0, 34):
        color = ["Red", "Green", "Blue", "White", "Black", "Yellow", "Purple", "Orange", "Pink"]
        animal = ["Cat", "Dog", "Snake", "Mouse", "Tiger", "Leopard", "Moose", "Wolf", "Bear"]
        number = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                  "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"]

        randomColor = random.randrange(0, len(color))
        randomAnimal = random.randrange(0, len(animal))
        randomNumber = random.randrange(0, len(number))

        password = "Password: " + color[randomColor] + animal[randomAnimal] + number[randomNumber]
        passwords.append(password)
    return passwords

list_two = password_generator()

for i in range(1,34):
    cellref = sheet.cell(row=i, column=6)
    cellref.value=list_two[i]

wb.save('excel_mod.xlsx')
input("Done")

I'm not really sure what the problem is. Why does it work with the first function but not the second one? I've tried different shells but it gives me the same error so I'm guessing the code is wrong in some way.

You do list_two = password_generator(), but then the very first line in that function is passwords = list_two(), but list_two is not yet defined at that point -- it will only have a value after the function has finished. Also, it will then probably be a list, and thus is not callable. You probably wanted to initialize it as an empty list, e.g. use either passwords = [] or passwords = list(), like you did in name_generator.

Since other answers already took care of why the name error happens, this answer just serves to clarify the difference between the first function and the second function as is asked in this question:

Why does it work with the first function but not the second one?

#first function
def name_generator():
    usernames = list()             #<--- makes a list
    ...
    ...
    return usernames
list = name_generator()            #<--- assigns to a variable NAMED list
#second function
def password_generator():
    passwords  = list_two()        #<--- ERROR: list_two is not defined as a function prior to this line
    ...
    ...
    return passwords
list_two = password_generator()    #<--- assigns to a variable NAMED list_two

What is the full error message that it gives you?

Also, it is recommended not to use list as a variable name.

For example:

list in list = name_generator(). It is better to use a more descriptive variable name, you will thank yourself when you need to look back on your code, or if someone else needs to read your code.

Also, just use usernames = [] to create an empty list type.

Comments
  • Please don't post the same question again and again, it is much better to update the question based on the feedback you receive.
  • Thank you! Didn't realize that list was a in-built function already!
  • This is the full error message:Traceback (most recent call last): File "E:\Desktopy", line 47, in <module> list_two = password_generator() File "E:\Desktop\y", line 32, in password_generator passwords = list_two() NameError: name 'list_two' is not defined
  • You don't have a function defined 'list_two()' anywhere. You are just calling the function 'password_generator()' with 'list_two = password_generator()' and assigning it's return value here. Do you mean 'passwords = []'?
  • list is not a reserved keyword in python. See this SO question:stackoverflow.com/questions/14694559/…. Please edit your answer so as to not misinform other readers.