I want my for loop to change the variable if there is a KeyError
,
numlist1 = {"one": 1}
numlist2 = {"two": 3}
numlist3 = {"three": 3}
userinputfix = ["two", "five"]
newlist = []
for x in userinputfix:
newlist.append(numlist1[x])
So if there is no "two" in numlist1, it should repeat the loop replacing numlist1 with numlist2, numlist3,...
I tried to use handling exceptions method:
numlist1 = {"one": 1}
numlist2 = {"two": 3}
numlist3 = {"three": 3}
userinputfix = ["two", "five"]
y = 1
newlist = []
for x in userinputfix:
try:
newlist.append(numlist{y}[x])
except KeyError:
y += 1
Something like the above but my code is not correct.