0

Good Morning,

I did not understand the python indentation in the following code:

for pessoa in lista_nomes:
    if pessoa['nome'] == nome:
        return pessoa
else:
    return {'nome': 'Não selecionado', 'idade': 0}

Why is else closing the indentation at the 'for' instead of the the 'if' level?

PythonNerd
  • 293
  • 1
  • 11

1 Answers1

3

It's because there is for/else in python

The else clause executes after the loop completes normally.

Check here for reference : https://book.pythontips.com/en/latest/for_-_else.html