Programming noob here. All the code I have encountered thus far, generally has if, elif, and else statements nested within some kind of loop. However this code that I have stumbled across has an else statement outside of the for loop, with no preceding if statements at the same indentation.
For some context, the code is an excerpt of a prefix trie.
for c in head.childs:
if c.value == l and c.reference != ref:
c.updateRef(ref)
return c
elif c.value == l:
return c
else:
n = Node(l, ref)
head.childs.append(n)
return n
I tried to test my understanding of the code by changing it to:
if len(head.childs) == 0:
n = Node(l, ref)
head.childs.append(n)
return n
for c in head.childs:
if c.value == l and c.reference != ref:
c.updateRef(ref)
return c
elif c.value == l:
return c
However, this causes it to crash.
Could someone please help me understand how this else statement can be outside of the for loop without any preceding if statements with the same indent? Thanks