Why does this `else` block work yet it is not on the same level as the `if` case? [duplicate]
问题 This question already has answers here : Why does python use 'else' after for and while loops? (21 answers) Closed 2 years ago . This code runs pretty well and generates the wanted list of prime numbers. But the else block that prints our prime numbers is out of block, but it works anyway, can someone explain it to me? for num in range(0, 100 + 1): # prime numbers are greater than 1 if num > 1: for i in range(2, num): if (num % i) == 0: break else: print(num) Reference: programiz.com 回答1: