# Search with elsedeffind_prime_factor(n):
for i inrange(2, int(n**0.5) + 1):
if n % i == 0:
return i
return n # n is prime# for/else: else runs only if loop completes without break
numbers = [15, 7, 20, 11]
for n in numbers:
for i inrange(2, n):
if n % i == 0:
print(f"{n} is composite (divisible by {i})")
breakelse:
print(f"{n} is prime")
Output
Click "Run" to execute your code
The else clause of a for loop runs only when the loop finishes without hitting break. This is a clean search pattern.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?