Skip to content

Instantly share code, notes, and snippets.

@Yunkou
Last active August 30, 2017 18:47
Show Gist options
  • Save Yunkou/e56804a11fa3831e427f994f982764c7 to your computer and use it in GitHub Desktop.
Save Yunkou/e56804a11fa3831e427f994f982764c7 to your computer and use it in GitHub Desktop.
For ... else
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