我不理解 Python 中的这种缩进 [重复]

Posted

技术标签:

【中文标题】我不理解 Python 中的这种缩进 [重复]【英文标题】:I don't Understand this Indentation in Python [duplicate] 【发布时间】:2020-03-28 04:02:33 【问题描述】:

早安,

我没看懂下面代码中的python缩进:

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

为什么要在“for”而不是“if”级别关闭缩进?

【问题讨论】:

【参考方案1】:

因为python中有for/else

else 子句在循环正常完成后执行。

在这里查看参考:https://book.pythontips.com/en/latest/for_-_else.html

【讨论】:

很好,谢谢。

以上是关于我不理解 Python 中的这种缩进 [重复]的主要内容,如果未能解决你的问题,请参考以下文章