-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisPhoneNumber.py
More file actions
22 lines (21 loc) · 790 Bytes
/
isPhoneNumber.py
File metadata and controls
22 lines (21 loc) · 790 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def isPhoneNumber(text):
if len(text) != 12:
return False # not phone number-sized
for i in range(0, 3):
if not text[i].isdecimal():
return False # not an area code
if text[3] != '-':
return False # does not have first hyphen
for i in range(4, 7):
if not text[i].isdecimal():
return False # does not have first 3 digits
if text[7] != '-':
return False # does not have second hyphen
for i in range(8, 12):
if not text[i].isdecimal():
return False # does not have last 4 digits
return True # "text" is a phone number!
print('415-555-4242 is a phone number:')
print(isPhoneNumber('415-555-4242'))
print('Moshi moshi is a phone number:')
print(isPhoneNumber('Moshi moshi'))