$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · hard · #73

What is the correct fix?

fns = []
for i in range(3):
    fns.append(lambda: i)
print(fns[0]())
  1. Bind it now: lambda i=i: i in append
  2. Bind it now: lambda i: i in the append
  3. Move the append above the for header
  4. Change range(3) to list(range(3))
The lambdas close over the variable, not its value, so all see the final i; a default binds it.
Play Problem Solving ▸