[英]How do I call a function based on user input in Python 3?
I am currently trying to create a program that learns through user input, however it converts to a string automatically. 我目前正在尝试创建一个通过用户输入学习的程序,但是它会自动转换为字符串。
Here's the code. 这是代码。 I use the shelve module to store the commands for the code. 我使用搁置模块来存储代码命令。
ok = {str(name):func}
asd.update(ok)
print(asd)
data["cmd"] = asd
data.close()
The 'asd' list contains every command which has been extracted from the shelf. “ asd”列表包含从机架中提取的每个命令。 I want to update it and store it, so next time it updates when calling a command. 我要更新并存储它,因此下次调用命令时它会更新。
'func' is the variable that stores the name of the function am trying to call, but string objects cannot be called. 'func'是存储试图调用的函数名称的变量,但是不能调用字符串对象。
How do I solve this? 我该如何解决?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~
EDIT: This has been solved (I totally forgot about eval() ) 编辑:这已经解决(我完全忘记了eval())
Not sure what you're trying to achieve here, but from what I've understood you should have a look to eval()
不知道您要在这里实现什么,但是据我了解,您应该看看eval()
The eval() function evaluates the specified expression, if the expression is a legal Python statement, it will be executed. eval()函数计算指定的表达式,如果该表达式是合法的Python语句,它将被执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.