【发布时间】:2017-09-04 08:26:44
【问题描述】:
我在我的脚本中有一个地方,我要求用户提供一些反馈(选择脚本中的下一阶段),并且由于大量的项目用户正在做出决定,我将我的“if-else”部分包装成一个字典。我可以调用任何“普通”函数,但在很多情况下,我必须调用相同的函数但带有一些参数,但无法弄清楚如何将这些添加到我已经拥有的脚本正文中。它看起来像这样(我会尝试对其发表评论,以便您了解我的问题所在):
def runthis():
# getting user input
decide = input(str(
'Select option 1'
'Select option 2'
'Select option 3'
'Select option etc'
'Select option n'))
# each action listed here as a function without parentesis and arguments
options = {
'1': func1,
'2': func2,
'3': func3,
'etc': funcetc,
'n': funcn}
# go through the list and look for key matching user decision
# then run the value as a function - how to pass arguments when I need?
if decide in options.keys():
options[decide]()
else:
print('invalid entry')
runthis()
runthis()
谢谢,非常感谢任何帮助。
【问题讨论】:
-
看来我终于找到了解决办法:stackoverflow.com/questions/24580993/…
标签: python-3.x function dictionary arguments call