Python giving a key error while inside a Try