I wrote a dict:
msgs = {"msg_1" : "a", "msg_2" : "b", "msg_3" : "c"}
now I don't want to read in the dict manually, because I don't know how many elements will be in there (I read the dict from a file):
z = "msg_{j}"
for i in range(1, len(msgs) - 1):
y = z.format(j=i) # This makes it "msg_1", "msg_2", "msg_3", ...
print(msgs[y])
This throws a KeyError: msg_1
.
If I print out one-by-one it works just fine:
print(msgs["msg_1"])
print(msgs["msg_2"])
print(msgs["msg_3"])
... a
... b
... c
Any idea what the reason is? Why is this the case?
I tested all the functions and everything work just fine, until I get to the part with the loop (and if I use print() instead of the loop it works fine).