Skip to content

Instantly share code, notes, and snippets.

@jftuga
Created July 25, 2022 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jftuga/6e3468f84315ecf2284809c3ad785537 to your computer and use it in GitHub Desktop.
Save jftuga/6e3468f84315ecf2284809c3ad785537 to your computer and use it in GitHub Desktop.
Python For Else Loop
# the else below should really have been named 'nobreak'
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print(f"{n} = {x}*{int(n/x)}")
break
else: # only occurs when 'nobreak'
# loop fell through without finding a factor
print(f"{n} is a prime number")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment