In the code snippet below, else is appearing outside the for loop and if condition is inside the for loop. My understanding is that ‘else’ cannot exist without ‘if’. However, in this case the ‘if’ condition is inside a for loop and ‘else’ is outside which means the scope of ‘if’ is only limited to the loop and it should not be available outside. Could you please clarify? Please note that ‘if’ and ‘else’ are at different indentation. The code can be found under “break and continue” keyword topic under “learn-python-3-from-scratch” course.
n = 10
x = 4
found = False # This bool will become true once x in found
for i in range(0, n):
if x == i+1:
found = True
print(“Number found”)
break
else:
print(“Number not found”)
Type your question above this line.
Course: https://www.educative.io/collection/10370001/5473789393502208
Lesson: https://www.educative.io/collection/page/10370001/5473789393502208/4640037261541376