L = []
nmax = 30
for n in range(2, nmax):
for factor in L:
if n % factor == 0:
break
else: # no break
L.append(n)
print(L)
Does the else in the outer loop work if the if statement is not execute din the inner loop...are they really connected inspite being in different loops
The else isn't connected to the if statement but to the break statement.
The for/else syntax means that the else block will be executed if there is no break encountered inside the for loop.
Here is an example for ppl to understand it:
fruits = ["Orange", "Apple", "Banana", "Strawberry"]
def searchFruit(wanted):
for fruit in fruits:
if fruit == wanted:
break
else: # Can't find the wanted fruit
return False
# Found the wanted fruit
return True
searchFruit("Tomato") # Output : False
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.