我有一个Python程序需要接受用户输入。我将用户输入存储在一个名为"userInput"的字符串变量中。我想要能够调用用户输入的字符串...
userInput = input("Enter a command: ")
userInput()
由此,我得到了错误信息:TypeError: 'str' object is not callable。
目前,我的程序类似于以下操作:
userInput = input("Enter a command: ")
if userInput == 'example_command':
example_command()
def example_command():
print('Hello World!')
很明显,这不是处理大量命令的非常有效的方式。我想让str对象可调用-有什么方法可以做到这一点吗?