I am trying to call functions from a user input, but I am struggling to figure out how. For instance, if I run the program and enter "3* foo"
, I expect a return of the output ("333333333333333333")
, except I get a TypeError: 'str' object is not callable
; any thoughts?
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)()
error:
TypeError: 'str' object is not callable
You are trying to call a string, remove the double quotes from the value of the dictionary, it should be:
dic = {"1*": func1, "2*": func2, "3*": func3}