How to take user input as function input

how to get string input in python 3
python user input integer
python while loop user input
python user input choice
python input vs raw_input
python keyboard input
write a script which displays documentation of python's input function.
how to take input in new line in python

I am trying to make my function take an name from the user which would check if the name is in a whitelist before executing a function that prints draws out information from a pre-defined list of the same name but the entered input is being processed as a string by the function instead of the name of the list. How do I get it to take in the input as the name of the list?

hydrogen = ["Hydrogen", "H", "1", "1.0"]
helium = ["Helium", "He", "2", "4.0"]

universe = ["hydrogen", "helium"]

elementname_print = "Element Name: "
elementsymbol_print = "Element Symbol: "
atomicnumber_print = "Atomic Number: "
relativeatomicmass_print = "Relative Atomic Mass: "

def printelement(element):
  print(f" \n-------------------------")
  print(elementname_print + element[0])
  print(elementsymbol_print + element[1])
  print(atomicnumber_print + element[2])
  print(relativeatomicmass_print + element[3])
  print("-------------------------")

userinput = input("-->")
if userinput in universe:
  printelement(userinput)
else:
  print("Sorry that element cannot be found.")

Result:

--> hydrogen

Element Name: h

Element Symbol: y

Atomic Number: d

Relative Atomic Mass: r


You should, rather than defining your elements in global scope as hydrogen = ..., define them inside a dictionary keyed by their name.

elements = {"hydrogen": ["Hydrogen", "H", "1", "1.0"],
            "helium": ["Helium", "He", "2", "4.0"]}

the lookup then becomes much easier.

def print_element(element_name):
    element = elements[element_name]
    # the rest as written

Note that you can clean up your code quite a bit:

elements = {"hydrogen": ["Hydrogen", "H", "1", "1.0"],
            "helium": ["Helium", "He", "2", "4.0"]}

def print_element(element_name):
    element = elements[element_name]
    name, symbol, number, mass = element

    print(f"""
----------------------
Element Name:         {name}
Element Symbol:       {symbol}
Atomic Number:        {number}
Relative Atomic Mass: {mass}
----------------------""")

userinput = input("-->")
if userinput in elements:
    print_element(userinput)
else:
    print("Sorry that element cannot be found.")

There are ways to make your chosen solution work (eval will do it, but introduce huge security risks. globals() will do it, but introduce a large performance overhead), but they're all ugly. Writing an ugly hack is objectively worse than using the right approach in the first place

marks and result can be in block scope, and parameters are denoted using the same syntax as variable declaration, i.e. remove int marks  How to neatly take user input as function input. Learn more about function, arguments


You can eval the string input to the corresponding variable :

printelement(eval(userinput))

Rest code remains the same.

P.S : This is a quick hack, using eval is unsafe.

When input() function executes program flow will be stopped until the user has given an input. The text or message display on the output screen to ask a user to enter input value is optional i.e. the prompt, will be printed on the screen is optional. Whatever you enter as input, input function convert it into a string. The Scanner class is used to get user input, and it is found in the java.util package. To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation.


Basically your need to get a list corresponding to the user input. Use globals():

  lst = globals()[userinput]

So in your example, if user types in 'hydrogen', this will give the list hydrogen. Now do your printings.

Complete example:

hydrogen = ["Hydrogen", "H", "1", "1.0"]
helium = ["Helium", "He", "2", "4.0"]
universe = ["hydrogen", "helium"]

elementname_print = "Element Name: "
elementsymbol_print = "Element Symbol: "
atomicnumber_print = "Atomic Number: "
relativeatomicmass_print = "Relative Atomic Mass: "

def printelement(element):
  print(f" \n-------------------------")
  print(elementname_print + element[0])
  print(elementsymbol_print + element[1])
  print(atomicnumber_print + element[2])
  print(relativeatomicmass_print + element[3])
  print("-------------------------")

userinput = input("-->")
if userinput in universe:
  lst = globals()[userinput]
  printelement(lst)
else:
  print("Sorry that element cannot be found.")

Python 3 has a built-in function input() to accept user input. In Python 2, to accept user input we can use the following two functions: –. input([  The input from the user is read as a string and can be assigned to a variable. After entering the value from the keyboard, we have to press the “Enter” button. Then the input() function reads the value entered by the user. The program halts indefinitely for the user input. There is no option to provide timeout value.


These functions have an optional parameter, commonly known as prompt , which is a string that will be printed on the screen whenever the function is called. Get User Input. You have already learned that Console.WriteLine() is used to output (print) values. Now we will use Console.ReadLine() to get user input.. In the following example, the user can input his or hers username, which is stored in the variable userName.


Get user input with Python using the input() function. The user can enter keyboard input in the console. In this article you'll learn how to get keyboard input​. The main difference between those two functions is input() function automatically converts user input to appropriate type. i.e., If a user-entered string input() function converts it into a string, and if a user entered a number it converts to an integer. raw_input() convert every user input to string.


After entering the value from the keyboard, we have to press the “Enter” button. Then the input()  You can input both keys and values using input() method and process it to separate the keys and values. [code]class_list = dict() data = input('Enter name &; score