小编jus*_*mer的帖子

Python 3:使str对象可调用

我有一个Python程序,需要用户输入.我存储用户输入一个名为"userInput"的字符串变量.我希望能够调用用户输入的字符串...

userInput = input("Enter a command: ")
userInput()
Run Code Online (Sandbox Code Playgroud)

从这里,我得到错误:TypeError:'str'对象不可调用

目前,我有程序做这样的事情:

userInput = input("Enter a command: ")
if userInput == 'example_command':
    example_command()

def example_command():
     print('Hello World!')
Run Code Online (Sandbox Code Playgroud)

显然,这不是处理大量命令的非常有效的方法.我想让str obj可以调用 - 无论如何这样做?

python string object callable

7
推荐指数
1
解决办法
8186
查看次数

标签 统计

callable ×1

object ×1

python ×1

string ×1