简体   繁体   English

如何在Python 3中基于用户输入调用函数?

[英]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语句,它将被执行。

More information here 更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从用户那里获取输入并根据输入调用函数? - How do I get input from user and call a function based on the input? 根据用户输入调用带有参数的 Python 函数 - Call a Python function with arguments based on user input 如何根据用户输入运行 def function - How do I run a def function based on user input 如何根据 python 中的用户输入执行操作 - how can I do the action based on user input in python 我如何创建一个函数,该函数根据在Python中输入相同输入的次数,使用户输入具有不同的结果? - How do I create a function that allows for user input to have a different outcomes based on how many times the same input was entered in Python? 如何根据python中的输入更新函数参数? - How do I update function parameters based on input in python? 如何在python中通过用户输入调用函数? - How to call function by user input in python? Python:如何使用字典调用方法(字典中的值)以根据用户输入(字典中的键)在不同的函数中运行? - Python: How to use a dictionary to call methods (values in dictionary) to run based on user input (key in dictionary) in a different function? 如何根据用户输入更改 api 调用目标?(python) - how to change api call destination based on user input?(python) 如何使用户调用函数 - How do I make the user call for a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM