Skip to content

Instantly share code, notes, and snippets.

@cristianrasch
Created April 2, 2012 14:37
Show Gist options
  • Save cristianrasch/2283905 to your computer and use it in GitHub Desktop.
Save cristianrasch/2283905 to your computer and use it in GitHub Desktop.
Python's for/while else loops
# 15-19 validate US telephone numbers with optional area code
# 800-555-1212, 555-1212, and also (800) 555-1212
regex = re.compile(r'(\d{3}-|\(\d{3}\)\s)?\d{3}-\d{4}')
for s in ['800-555-1212', '555-1212', '(800) 555-1212']:
match = re.match(regex, s)
if not match:
print 'Error! %s should be a valid telephone number' % s
break
else:
print 'All telephone numbers are valid, yey!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment