ACarter
ACarter

Reputation: 5697

Python KeyError: '0' on printing a dictionary value

File "F:\ake\pa\th\help.py", line 268, in main
print type(outputs[x])
KeyError: '0'

My file is quite large, so these are the places outputs is seen, in order of how they should be executed:

outputs={}

#now in another function
global another_var,outputs

#if malarkey...
elif lines[row][col]=="0":
    outputs["0"]=str(state) #state is either 0 or 1

#back in original function
global outputs

for x in nums:
    print outputs[x], #line 268

I really am not sure why I am getting the error above.

Upvotes: 1

Views: 8427

Answers (1)

lvc
lvc

Reputation: 35059

It is difficult to tell without minimal code to try to run, but the most likely thing is that your elif lines[row][col] == "0": isn't triggering when you expect it to. Print something when you reach there to make sure it is.

Also, do print outputs immediately before the error (or, catch the error and print it then) to see exactly what is in it. Between those two things, you should see something different to what you're imagining is going on.

Upvotes: 2

Related Questions