Why KeyError: 0

I’m attempting to solve Project Euler 21 but I’m getting KeyError: 0 which normally occurs when you refer to a dictionary key that doesn’t exist. However, I thought I had solved that problem with the < 10000 condition. The error refers to the first ‘if’ statement in the main() function.

sumsdivs = {}
for i in range(1, 10000):
    tmpls = []
    for j in range(1, i):
        if i % j == 0:
    sumsdivs[i] = sum(tmpls)

amls = []
def main():
    for i in range(1, 10000):
        if sumsdivs[i] < 10000 and sumsdivs[i] == sumsdivs[sumsdivs[i]]:
            if sumsdivs[i] not in amls:
            if sumsdivs[sumsdivs[i]] not in amls:
    return sum(amls)


Any ideas?

Leave a Comment