python查询中的函数

2024-09-29 21:28:01 发布

您现在位置:Python中文网/ 问答频道 /正文

def directionToVector(direction, speed = 1.0):
    dx, dy =  Actions._directions[direction]
    return (dx * speed, dy * speed)

def getCostOfActions(self, actions):
    """
    Returns the cost of a particular sequence of actions.  If those actions
    include an illegal move, return 999999.  This is implemented for you.
    """
    if actions == None: return 999999
    x,y= self.startingPosition
    for action in actions:
      dx, dy = Actions.directionToVector(action)
      x, y = int(x + dx), int(y + dy)
      if self.walls[x][y]: return 999999
    return len(actions)

def getCostOfActions(self, actions):
     """
     actions: A list of actions to take

     This method returns the total cost of a particular sequence of actions.
     The sequence must be composed of legal moves
     """

 File "in getCostOfActions
    dx, dy = Actions.directionToVector(action)
  File  in directionToVector
    dx, dy =  Actions._directions[direction]
KeyError: 'N'

我正在使用最后一个函数,但函数不接受参数。这里的论据应该是什么?他们的类型应该是什么?在


Tags: ofinselfactionsreturndefactionspeed
1条回答
网友
1楼 · 发布于 2024-09-29 21:28:01

第一个参数,当然是self,因为这是一个实例方法。这是隐式传递的。在

根据docstring,调用者应该提供的参数是“actions:A列出要采取的操作”。在

例如:

instance.getCostOfActions([North, East, South, West])

注:你有两条线,因此:

^{pr2}$

第一个被第二个取代。在

相关问题 更多 >

    热门问题