Skip to content

Instantly share code, notes, and snippets.

@serpent213
Created December 9, 2018 18:47
Show Gist options
  • Save serpent213/6a45ed6adf8797fef9cccb8a5b0db2ab to your computer and use it in GitHub Desktop.
Save serpent213/6a45ed6adf8797fef9cccb8a5b0db2ab to your computer and use it in GitHub Desktop.
validate phone number
#!/usr/bin/env python
def isPhoneNumber(text):
if len(text) != 11:
return False
for i in range(0,3):
if not text[i].isdigit():
return False
if text[3] != '-':
return False
for i in range(4,7):
if not text[i].isdigit():
return False
if text[7] != '-':
return False
for i in range(8,11):
if not text[i].isdigit():
return False
return True
print('666 666 666 is a phone number: ')
print(isPhoneNumber('666-666-666'))
print('Moshi moshi is a phone number')
print(isPhoneNumber('Moshi moshi'))
@serpent213
Copy link
Author

Play around with the interactive shell to see how exactly the ranges expand:

% python                                                                                                                                                                                                !2070
Python 2.7.10 (default, Aug 17 2018, 17:41:52) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> range(0,3)
[0, 1, 2]
>>> range(4,7)
[4, 5, 6]
>>> range(8,11)
[8, 9, 10]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment