【问题标题】:python variable input custom function callingpython变量输入自定义函数调用
【发布时间】:2017-02-22 01:39:33
【问题描述】:

我正在尝试从用户输入调用函数,但我正在努力弄清楚如何。例如,如果我运行程序并输入"3* foo",我希望返回输出("333333333333333333"),除非我得到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

【问题讨论】:

    标签: function python-3.x variables calling-convention


    【解决方案1】:

    你试图调用一个字符串,从字典的值中去掉双引号,应该是:

    dic = {"1*": func1, "2*": func2, "3*": func3}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      相关资源
      最近更新 更多