2
while stack.isEmpty() != 1:
             fin = stack.pop()
         print fin            - output is (1,1)
         k = final.get(fin)
         return k

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

but when I do this return, it gives me an error and final is the directory that I have made with lists of keys and values

The error is:

File "line 212, in directionToVector
    dx, dy =  Actions._directions[direction]
KeyError: 'W'
7
  • I think while not stack.isEmpty(): looks a bit better Commented Jul 18, 2010 at 9:01
  • Or while stack: assuming it is a list. Commented Jul 18, 2010 at 9:04
  • ys...bt it doesnt make n]any differnce in the output Commented Jul 18, 2010 at 9:05
  • Can you post more of the code before while stack.isEmpty() != 1: ? And where do you call directionToVector ? Commented Jul 18, 2010 at 9:06
  • I'm not calling diretionToVector anywhere in my code...there is a file in which this func is already defined.There are lots of functions that are in other files that are running like...illegal direction, total cost...etc... Commented Jul 18, 2010 at 9:17

2 Answers 2

1

Actions._directions is presumably a dictionary, so the line:

dx, dy =  Actions._directions[direction]

at runtime (based on the error message) is:

dx, dy =  Actions._directions["W"]

and it's complaining that there's no key "W" in that dictionary. So you should check to see that you've actually added that key with some value in there. Alternatively, you can do something like:

dx, dy =  Actions._directions.get(direction, (0, 0))

where (0, 0) can be any default value you choose when there's no such key. Another possibility is to handle the error explicitly:

try:
    dx, dy =  Actions._directions[direction]
except KeyError:
    # handle the error for missing key
Sign up to request clarification or add additional context in comments.

Comments

0

This error

KeyError: 'W'

means that the key you requested ('W') is not one of the keys that are stored in the dictionary. This is because your dictionary key is 'west' not 'W' (see your previous question). Try this instead:

key = { 'N' : 'north', 'S' : 'south', 'E' : 'east', 'W' : 'west' }[direction]
dx, dy =  Actions._directions[key]

Alternatively, make sure you pass the string 'west' to directionToVector and not the string 'W'.

5 Comments

But I have to keep the key as(1,1), (4,5)..in this format so that I can go back to my path by referencing this key and get the direction related to this key. I cant change the finction "def directionvector"...I am not allowed to do that....
I think you're confusing keys with values. The keys are "west", "north", etc, and the values are (1, 1), (4, 5), etc.
@Shilpa: Why are you not allowed to change the definition? Your teacher said this? Besides you don't need to change the definition, just how it is called.
my teacher told me to make changes in only 1 file....dont touch other files...Bcoz they asked me to submit my code only..and they will run it in their system with other files keeping unchanged...
my dict output is in this order: {(5, 4): 'North', (1, 3): 'West', (5, 5): 'North', (4, 5): 'East'............so on....so here the key is (5,4) and value is North...is it right??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.