0

I am trying to understand why newNameList is not defined:

ListofNames1 = ['Mark', 'Andrew']

ListofNames2 = ['Anjela', 'Lora']

names = ListofNames1

def greeting(names):
    newNameList = []
    for item in names:
        newNameList.append(str(names))
    return (names)
print(greeting(names))

def function2(newNameList):
    for each in newNameList:
        newNameList2.append(newNameList.upper())
    return (newNameList2)

print(function2(newNameList)) 

The output

['Mark', 'Andrew']
...
NameError: name 'newNameList' is not defined.

The name error occurs on the last line in the code.

3
  • Perhaps providing some more context would be useful so here isthe code: Commented Mar 6, 2020 at 14:29
  • def greeting(names): newNameList = [] for item in names: newNameList.append(str(names)) return (names) print(greeting(names)) def function2(newNameList): for each in newNameList: newNameList2.append(newNameList.upper()) return (newNameList2) print(function2(newNameList)) The output ['Mark', 'Andrew'] newNameList2.append(newNameList.upper()) return (newNameList2) ---> print(function2(newNameList)) NameError: name 'newNameList' is not defined So I am not sure what I am missing there Commented Mar 6, 2020 at 14:36
  • 2
    Please add your code to your question and format it correctly to allow users to help you. Else, it's simply impossible to do so. Commented Mar 6, 2020 at 16:18

3 Answers 3

1

newNameList is only defined within the scope of function2. Since the print statement is not indented at the same level of function2 then newNameList is not visible to it. The three variables defined at a top-level scope are ListofNames1, ListofNames1, and names. These are the only three variables that can be passed to function2 in the print statement.

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, You can do it.

For example:

def use_greeting_function(name):
   new_list_name = greeting(name)

Now new_list_name has the output of greeting function and you can use it in the function afterwards.

3 Comments

def greeting(names): newNameList = [] for item in names: newNameList.append(str(names)) return (names) print(greeting(names)) def function2(newNameList): for each in newNameList: newNameList2.append(newNameList.upper()) return (newNameList2) print(function2(newNameList)) The output ['Mark', 'Andrew'] newNameList2.append(newNameList.upper()) return (newNameList2) ---> print(function2(newNameList)) NameError: name 'newNameList' is not defined So I am not sure what I am missing there
add the code with proper formatting in the question itself.
Sorry, legit my first time using this, maybe look at the image I just uploaded helps ? Thank you for your time though
0
NameError: name 'newNameList' is not defined. 

tells you what's wrong. You should have defined newNameList outside the greetings() function.

I have rewritten your code:

ListofNames1 = ['Mark', 'Andrew']
ListofNames2 = ['Anjela', 'Lora']
names = ListofNames1

newNameList = []

def greeting(names):
    for item in names:
        newNameList.append(names)

    return names

print(greeting(names))


def function2(newNameList):
    newNameList2 = []

    for each in newNameList:
        newNameList2.append(str(newNameList).upper())

    return newNameList2

print(function2(newNameList))

And using the upper() method on a list doesn't work. Convert it to str first.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.