Skip to main content
๐Ÿ› Bug HuntPY Python Hard+35 XP

Scope Error in Loop

This should create 3 functions that print 0, 1, 2 but all print 2.

๐Ÿ› Find the bug
1functions = []
2for i in range(3):
3 functions.append(lambda: print(i))
4
5for f in functions:
6 f()
๐Ÿ› The bug is on line 3

๐Ÿ”ง Select the correct fix:

Priygop - Leading Professional Development Platform | Expert Courses & Interview Prep