Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python—作为参数传递给函数的变量名称的用户输入_Python_Function_Global Variables_User Input - Fatal编程技术网

Python—作为参数传递给函数的变量名称的用户输入

Python—作为参数传递给函数的变量名称的用户输入,python,function,global-variables,user-input,Python,Function,Global Variables,User Input,我在这里有一个问题,它是基于用户对脚本的输入,并将此用户输入传递给函数 我有一个脚本,在其中我定义了一个函数。我想要脚本做的是将用户输入作为参数传递给函数。但是,我想传递给函数的一件事是参数的名称,而不是参数本身。用户可以选择使用各种不同的列表来输入函数,我想要的是让用户输入他们想要使用的变量的名称 假设我想将参数Tablelist3传递给函数。当我请求用户输入时,他们输入Tablelist3,传递给函数的是作为字符串的“Tablelist3”,而不是变量本身 我如何获得它,以便用户名中的任何变

我在这里有一个问题,它是基于用户对脚本的输入,并将此用户输入传递给函数

我有一个脚本,在其中我定义了一个函数。我想要脚本做的是将用户输入作为参数传递给函数。但是,我想传递给函数的一件事是参数的名称,而不是参数本身。用户可以选择使用各种不同的列表来输入函数,我想要的是让用户输入他们想要使用的变量的名称

假设我想将参数Tablelist3传递给函数。当我请求用户输入时,他们输入Tablelist3,传递给函数的是作为字符串的“Tablelist3”,而不是变量本身

我如何获得它,以便用户名中的任何变量都是传递给函数的变量

希望我的问题有道理,不要太简单。我对Python相对缺乏经验。

对于原始输入,输入只是一个字符串

使用input()对用户提供的输入数据进行评估,确保在请求用户输入之前声明了所有可能的输入列表,否则会出现
NameError

函数的作用是:返回模块中定义的所有对象的字典。 通过将变量名作为键传递,可以获得相应的对象。

对于原始输入,输入只是一个字符串

使用input()对用户提供的输入数据进行评估,确保在请求用户输入之前声明了所有可能的输入列表,否则会出现
NameError

函数的作用是:返回模块中定义的所有对象的字典。
通过将变量名作为键传递,可以获得相应的对象。

使用字典,将字符串映射到对象:

tbl1, tbl2 = [1 ,2 ,3], [4 ,5 ,6]
args = {'tbl1': tbl1 ,"tbl2" :tbl2}
# show tables ......

inp = input("Choose table")


def foo(var):
    print(var)


foo(args[inp])

您需要执行错误检查,以确保用户实际输入的内容有效。

使用字典,将字符串映射到对象:

tbl1, tbl2 = [1 ,2 ,3], [4 ,5 ,6]
args = {'tbl1': tbl1 ,"tbl2" :tbl2}
# show tables ......

inp = input("Choose table")


def foo(var):
    print(var)


foo(args[inp])

您需要执行错误检查,以确保用户实际输入的内容有效。

我认为您可以利用python的全局()或局部()功能

下面是一个例子:

from random import choice as rc



def your_function(variable):
    if variable in local_keys:
        print(variable, " = ", local_variables[variable])
    else:
        print("Your input is not a defined variable")
    return

#randomly populate the variables
item1 = rc(range(20))
item2 = rc(range(20))
item3 = rc(range(20))
item4 = rc(range(20)) 
item5 = rc(range(20))
item5 = rc(range(20))

user_variable = input("Input the variable name: ")

local_variables = locals() # you may have to use globals instead
local_keys = list(local_variables.keys())

your_function(user_variable)

我认为您可以利用python的global()或locals()特性

下面是一个例子:

from random import choice as rc



def your_function(variable):
    if variable in local_keys:
        print(variable, " = ", local_variables[variable])
    else:
        print("Your input is not a defined variable")
    return

#randomly populate the variables
item1 = rc(range(20))
item2 = rc(range(20))
item3 = rc(range(20))
item4 = rc(range(20)) 
item5 = rc(range(20))
item5 = rc(range(20))

user_variable = input("Input the variable name: ")

local_variables = locals() # you may have to use globals instead
local_keys = list(local_variables.keys())

your_function(user_variable)