python变量输入自定义函数调用
python variable input custom function calling
我正在尝试从用户输入中调用函数,但我正在努力弄清楚如何操作。例如,如果我 运行 程序并输入 "3* foo"
,我期望输出 ("333333333333333333")
的 return,除了我得到 TypeError: 'str' object is not callable
;有什么想法吗?
def func3():
print ("333333333333333333")
command="3* foo" #command would be an input usually
f=command.split()
dic="1*":"func1", "2*":"func2", "3*":"func3"}
function_caller=(dic[f[0]])
(function_caller)()
错误:
TypeError: 'str' object is not callable
你正在尝试调用一个字符串,从字典的值中去掉双引号,它应该是:
dic = {"1*": func1, "2*": func2, "3*": func3}
我正在尝试从用户输入中调用函数,但我正在努力弄清楚如何操作。例如,如果我 运行 程序并输入 "3* foo"
,我期望输出 ("333333333333333333")
的 return,除了我得到 TypeError: 'str' object is not callable
;有什么想法吗?
def func3():
print ("333333333333333333")
command="3* foo" #command would be an input usually
f=command.split()
dic="1*":"func1", "2*":"func2", "3*":"func3"}
function_caller=(dic[f[0]])
(function_caller)()
错误:
TypeError: 'str' object is not callable
你正在尝试调用一个字符串,从字典的值中去掉双引号,它应该是:
dic = {"1*": func1, "2*": func2, "3*": func3}