else 是否在 for 循环之外,如果是,为什么 else 不执行?
Is else outside for loop, if yes why else is not executing?
numbers = [10, 20, 33, 55, 39, 55, 75, 37, 21, 23, 41, 13]
for num in numbers:
if num % 2 == 0:
print(num)
break
else:
print(num)
在上面的代码中,我确实有 else
块对应于 for
循环并且没有被执行。有人可以指导我为什么它不执行吗?
没有其他 "if"。我想你需要这样的东西:
numbers = [10, 20, 33, 55, 39, 55, 75, 37, 21, 23, 41, 13]
for num in numbers:
if num % 2 == 0:
print(num)
break
print(num)
缩进好像不对,试试这个
for num in numbers:
if num % 2 == 0:
print(num)
break
else:
print(num)
是的,else
块对应于 for
循环,但它只会在 break
永远不会执行的情况下执行。由于您在 numbers
列表中有偶数,因此执行中断,这就是为什么 else
未执行
for num in numbers:
if num % 2 == 0:
print(num)
break
else:
print(num)
尝试使用此列表 number=[11,22,33]
,else
块将被执行,以获取更多信息
4.4. break and continue Statements, and else Clauses on Loops
Python 有不同的语法,其中循环语句可能有一个 else 子句
Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the iterable (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement. This is exemplified by the following loop, which searches for prime numbers:
我认为最好用不同的测试来证明这一点。所以一个 for
循环可以有一个 else
块。 else
块只有在循环正常完成时才会执行。即循环中没有 break
。如果我们创建一个接受列表和分隔符的函数。我们可以看到,如果 if
条件匹配并且我们打印然后 break,else
块永远不会是 运行。只有当我们 运行 在没有 break
的情况下一直执行循环时,才会执行 else
def is_divisable_by(nums, divider):
for num in nums:
if num % divider == 0:
print(num, "is divsiable by ", divider)
break
else:
print("none of the numbers", nums, "were divisable by", divider)
numbers = [1, 6, 3]
numbers2 = [7, 8, 10]
is_divisable_by(numbers, 2)
is_divisable_by(numbers, 7)
is_divisable_by(numbers2, 4)
is_divisable_by(numbers2, 6)
输出
6 is divsiable by 2
none of the numbers [1, 6, 3] were divisable by 7
8 is divsiable by 4
none of the numbers [7, 8, 10] were divisable by 6
numbers = [10, 20, 33, 55, 39, 55, 75, 37, 21, 23, 41, 13]
for num in numbers:
if num % 2 == 0:
print(num)
break
else:
print(num)
在上面的代码中,我确实有 else
块对应于 for
循环并且没有被执行。有人可以指导我为什么它不执行吗?
没有其他 "if"。我想你需要这样的东西:
numbers = [10, 20, 33, 55, 39, 55, 75, 37, 21, 23, 41, 13]
for num in numbers:
if num % 2 == 0:
print(num)
break
print(num)
缩进好像不对,试试这个
for num in numbers:
if num % 2 == 0:
print(num)
break
else:
print(num)
是的,else
块对应于 for
循环,但它只会在 break
永远不会执行的情况下执行。由于您在 numbers
列表中有偶数,因此执行中断,这就是为什么 else
未执行
for num in numbers:
if num % 2 == 0:
print(num)
break
else:
print(num)
尝试使用此列表 number=[11,22,33]
,else
块将被执行,以获取更多信息
4.4. break and continue Statements, and else Clauses on Loops
Python 有不同的语法,其中循环语句可能有一个 else 子句
Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the iterable (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement. This is exemplified by the following loop, which searches for prime numbers:
我认为最好用不同的测试来证明这一点。所以一个 for
循环可以有一个 else
块。 else
块只有在循环正常完成时才会执行。即循环中没有 break
。如果我们创建一个接受列表和分隔符的函数。我们可以看到,如果 if
条件匹配并且我们打印然后 break,else
块永远不会是 运行。只有当我们 运行 在没有 break
的情况下一直执行循环时,才会执行 else
def is_divisable_by(nums, divider):
for num in nums:
if num % divider == 0:
print(num, "is divsiable by ", divider)
break
else:
print("none of the numbers", nums, "were divisable by", divider)
numbers = [1, 6, 3]
numbers2 = [7, 8, 10]
is_divisable_by(numbers, 2)
is_divisable_by(numbers, 7)
is_divisable_by(numbers2, 4)
is_divisable_by(numbers2, 6)
输出
6 is divsiable by 2
none of the numbers [1, 6, 3] were divisable by 7
8 is divsiable by 4
none of the numbers [7, 8, 10] were divisable by 6