Accessing Lists Defined inside Function

python pass list to function as arguments
python list
list comprehension || python
accessing list of lists python
nested list
python list of lists
nested list python
python assign list values to variables

When running the code it asks for input according to plan, however the list alpha is not defined.

First the input did not read so I changed the indentations so that it did, however the defining issue is not solved.

def newclassification1(alpha,bravo):
        alpha = ["Jacob", "Jane", "Jim"]
        bravo = ["Male", "Female", "Unknown"]

    name = input("What is the persons name?")
    if name in alpha:
        while True:
            print(bravo[alpha.index(name)])
    else:
        print("The persons name is not in the register.")

Error message:

Traceback (most recent call last): File "", line 7, in NameError: name 'alpha' is not defined

I don't know your final purpose but these two codes work without any errors on Python3.

The first one:

def newclassification1(alpha,bravo):
    name = input("What is the persons name?")
    if name in alpha:
        while True:
            print(bravo[alpha.index(name)])
    else:
        print("The persons name is not in the register.")

newclassification1(["Jacob", "Jane", "Jim"], ["Male", "Female", "Unknown"])

The second one:

def newclassification1(alpha,bravo):
    alpha = ["Jacob", "Jane", "Jim"]
    bravo = ["Male", "Female", "Unknown"]
    name = input("What is the persons name?")
    if name in alpha:
        while True:
            print(bravo[alpha.index(name)])
    else:
        print("The persons name is not in the register.")

newclassification1([], [])

11. Lists, We will use the term element or item to mean the same thing. The syntax for accessing the elements of a list is the same as the syntax for In the function above, the parameter a_list and the variable things are aliases for the same object. I have a list that I create inside of function1.I want to be able to access and modify it in function2.How can I do this without a global variable? Neither function is nested within the other and I need to be able to generalize this for multiple lists in several functions.

As pointed by the other people, you should fix the indentation, as well as figure out something with your arguments. First of all, when you define alpha and bravo in the function, each time it is called they will be rewritten, which I suppose you don't want to happen. Secondly, you should fix the indentation. I suggest to have the following:

def newclassification1(alpha=["Jacob", "Jane", "Jim"],bravo=["Male", "Female", "Unknown"]):
        name = input("What is the persons name?")
        if name in alpha:
            print(bravo[alpha.index(name)])
        else:
            print("The persons name is not in the register.")

This way, by default you will be using your alpha=["Jacob", "Jane", "Jim"] and bravo=["Male", "Female", "Unknown"] but easily change it to whatever you want later when you call the function.

Also, you don't need a while True part as it will run without stopping the execution

An Introduction to Python Lists, If you pass in another list, the list function makes a copy. L[-1] can be used to access the last item in a list. sort the list. These operations are described below​. Inner functions. A function which is defined inside another function is known as inner function or nested function. Nested functions are able to access variables of the enclosing scope. Inner functions are used so that they can be protected from everything happening outside the function. This process is also known as Encapsulation.

Extending Celius answer:

alpha = ["Jacob", "Jane", "Jim"]
bravo = ["Male", "Female", "Unknown"]
def newclassification1(alpha,bravo):
    name = input("What is the persons name?")
    print(bravo[alpha.index(name)] if (name in alpha) else "xxxxx")
newclassification1(alpha,bravo)

Just using Python ternary operation and sending result directly to print statement

Python List, How do you access a list inside a list in Python? Python Exercises, Practice and Solution: Write a Python program to access a function inside a function.

I made some adjustments, the main is removing alpha and bravo from the function.

alpha = ["Jacob", "Jane", "Jim"]
bravo = ["Male", "Female", "Unknown"]
def newclassification1(alpha,bravo):
    name = input("What is the persons name?")
    if name in alpha:
        print(bravo[alpha.index(name)])
    else:
        print("The persons name is not in the register.")
newclassification1(alpha,bravo)

This works, because since you are passing alpha and bravo to the function, these variables should be defined outside the function. Also I changed your while into an if because otherwise, you would get infinite outputs. Output for the function I'm sharing:

What is the persons name?Jim
Unknown
What is the persons name?Jane
Female
What is the persons name?a
The persons name is not in the register.

Given OPs comment: alpha and bravo must be defined within the fucntion.

def newclassification1(alpha = [],bravo = []):
    alpha = ["Jacob", "Jane", "Jim"]
    bravo = ["Male", "Female", "Unknown"]
    name = input("What is the persons name?")
    if name in alpha:
        print(bravo[alpha.index(name)])
    else:
        print("The persons name is not in the register.")
newclassification1()

Output:

What is the persons name?Jim
Unknown
What is the persons name?Tim
The persons name is not in the register.
What is the persons name?Jane
Female

Python, How do you access a variable inside a function in Python? Lists are the R objects which contain elements of different types like − numbers, strings, vectors and another list inside it. A list can also contain a matrix or a function as its elements. List is created using list() function. Creating a List. Following is an example to create a list containing strings, numbers, vectors and a logical values.

def newclassification1(name):
    alpha = ["Jacob", "Jane", "Jim"]
    bravo = ["Male", "Female", "Unknown"]
    if name in alpha:
        print(bravo[alpha.index(name)])
    else:
        print("The persons name is not in the register.")

name = input("What is the persons name?")

newclassification1(name)

Python list() Function, Functions which take lists as arguments and change them during execution are below takes a list as an argument and multiplies each element in the list by 2:. Variable defined inside the class: The variables that are defined inside the class but outside the method can be accessed within the class(all methods included) using the instance of a class. For Example – self.var_name. If you want to use that variable even outside the class, you must declared that variable as a global. Then the variable can be accessed using its name inside and outside the class and not using the instance of the class.

10.18. Using Lists as Parameters, The variables that are defined inside the class but outside the method can be accessed within the class(all methods included) using the instance of a class. The use of closures and factory functions is the most common and powerful use for inner functions. In most cases, when you see a decorated function, the decorator is a factory function that takes a function as argument and returns a new function that includes the old function inside the closure. Stop. Take a deep breath. Grab a coffee. Read

Python Inner Functions—What Are They Good For? – Real Python, This means that they are on par with any other object (integers, strings, lists, In other words, we could have defined these functions in another way (as discussed​). function to check if a certain user has the correct permissions to access a  C++ class and functions: Inside the class definition As the name suggests, here the functions are defined inside the class. Functions defined inside the class are treated as inline functions automatically if the function definition doesn’t contain looping statements or complex multiple line operations.

Python List Methods, In this reference page, you will find all the list methods to work with Python lists. to add a single item to the end of the list, you can use the list.append() method.

Comments
  • To clarify: def newclassification1(alpha,bravo): alpha = ["Jacob", "Jane", "Jim"] ; bravo = ["Male", "Female", "Unknown"] ;
  • Why do you have alpha and bravo as function arguments and then assign values to each of them as the first step inside your function?
  • The indentation is still broken (but now a combination of the original code and benvc's attempt at fixing it).
  • Dont know im a n00b.
  • If you really want to (re)define the structures inside the function, you can assign them default values to None in the arguments, like def newclassification1(alpha = None, bravo = None)
  • Careful with the while loop, it prints an infinite repetition.
  • What's with the if True: tho, you need to remove it. Just because op has incorrect code you don't need a filler for that, when you are writing answers you should just delete the unnecessary code.
  • You are right, I wasn't paying attention to the whole code, just saw the issue with the while and thought it was a condition that was to be applied once, so changed it to an if without checking that there was an if in the line above!
  • @Celius Stingher; Global variables arent allowed thats why I had the lists inside the function. How would you solve it with the list in the same function alternatively making a separate function for the list and then call that function when needed.
  • What you can do is start by passing lists as optional arguments. Check the edited answer.
  • You should probably add some explanation to your answer to explain what you did to fix the problem.