Created
July 25, 2022 12:55
-
-
Save jftuga/6e3468f84315ecf2284809c3ad785537 to your computer and use it in GitHub Desktop.
Python For Else Loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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