tags:

views:

57

answers:

1
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'

I am using the last function but the arguments are not accepted by the function. What should be the arguments here? What should their types be?

A: 

The first argument, of course is self, since this is an instance method. This is passed implicitly.

According to the docstring, the argument the callers should provide is 'actions: A list of actions to take'.

E.g:

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

N.B: You have two lines thus:

def getCostOfActions(self, actions):
def getCostOfActions(self, actions):

The first is replaced by the second.

Johnsyweb
but i have only 1 direction to pass...oi dont have the list of actions.
Shilpa
I'm usng this function in the for loop.So for a index- I have 1 direction
Shilpa
Then, since the function expects a *list* over which to iterate, you shall need to pass your action in a list by enclosing it in square brackets. For example: `instance.getCostOfActions([an_action])`
Johnsyweb
i run the 3 rd fuct...2nd is the desciption of 3rd one. it is in other file.
Shilpa
ok...let me c if it works
Shilpa
If these methods are in different files or even different classes, then you should make this abundantly obvious in your question!
Johnsyweb
"ok...let me c if it works – Shilpa 16 hours ago"Any joy?
Johnsyweb