Loop else block runs if and only if the loop is exited normally, i.e., without hitting a break.
The following code determines whether a positive integer y is prime by searching for factors greater than 1:
y = 13 x = y // 2 # For some y > 1 while x > 1: if y % x == 0: # Remainder print(y, 'has factor', x) break # Skip else x -= 1 # w w w . j av a 2 s . c o m else: # Normal exit print(y, 'is prime')
The code inserts a break where a factor is found.