Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用args从dict调用函数_Python 3.x - Fatal编程技术网

Python 3.x 使用args从dict调用函数

Python 3.x 使用args从dict调用函数,python-3.x,Python 3.x,我想从字典中调用函数 def target(Enemy): hero.target = Enemy commands = { 'close':close, 'weapon attack':wpnatk, 'spell attack':splatk, 'flee':flee, 'look':look, 't

我想从字典中调用函数

def target(Enemy):
hero.target = Enemy

commands = {
                'close':close,
                'weapon attack':wpnatk,
                'spell attack':splatk,
                'flee':flee,
                'look':look,
                'target':target
    }
    inp = str(input('>>'))
    if inp in commands:
        commands[inp]()

如何将参数推送到函数?如果我记得你没有在字典中添加括号。

只需在末尾添加括号:

def target(n):
    print(n)

functions = {
    'function':target
}

functions['function'](1)

您可以像对待“普通”函数一样“推送参数”:将其放在括号中。它可以从if语句调用。输入拆分是否会与命令[inp](inp[-1])一起工作,从而使输入的最后一部分成为参数?除了仅适用于“目标龙”之类的对象,但逃跑或关闭不会以这种方式工作