Last active
August 30, 2017 18:47
-
-
Save Yunkou/e56804a11fa3831e427f994f982764c7 to your computer and use it in GitHub Desktop.
For ... else
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
needle = 'd' | |
haystack = ['a', 'b', 'c'] | |
# The bad way | |
found = False | |
for letter in haystack: | |
if needle == letter: | |
print('Found!') | |
found = True | |
break | |
if not found: | |
print('No Found!') | |
# The good way | |
for letter in haystack: | |
if needle == letter: | |
print('Found!') | |
break | |
else: | |
print('No Found!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment