Skip to content

else字句何时执行 #2

Description

@quinnqi

Hi comyn:
我有一个逻辑问题没有搞懂。
代码如下:

for x in range(0, 10, 2):
    if x % 2 != 0:
        break
else:
    print('ok')

输出结果:ok

问题: for x in range(0, 10, 2): x的赋值应该是[2, 4, 6, 8],都是偶数,应该不满足if条件,不会break。为什么最终结果会返回ok呢?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions