我已经使用字典来允许用户输入内容,但下一个问题是使用第二个字作为被调用函数的参数。目前,我有:使用输入作为字典调用的函数的参数
def moveSouth():
Player.makeMove("south")
def moveNorth():
Player.makeMove("north")
def moveEast():
Player.makeMove("east")
def moveWest():
Player.makeMove("west")
function_dict = {'move south':moveSouth, 'wait':wait, 'sleep':sleep,
'move north':moveNorth, 'move':move, 'look':look,
'move east':moveEast,
'move west':moveWest}
而要获得输入:
command = input("> ")
command = command.lower()
try:
function_dict[command]()
except KeyError:
i = random.randint(0,3)
print(responses[i])
然而,而不是必须有4个不同的功能,使一招,我希望会有办法以便当用户输入“向南移动”时,它使用第一个字调用该功能,然后使用“南”作为该功能中方向的参数。
为什么你定义'moveWest'和其他呢? –