-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_phone_number.py
38 lines (30 loc) · 972 Bytes
/
check_phone_number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/python3
def main():
while True:
userInput = input('Enter a U.S. phone number: ').strip()
result = checkPhoneNum(userInput)
print(result)
def checkPhoneNum(phoneNum):
valid = '-() '
numbers = 0
for i in phoneNum:
if i.isnumeric():
numbers += 1
if 9 < numbers < 12:
tripleNumCount = 0
for count, i in enumerate(phoneNum):
if i not in valid and not i.isnumeric():
print(i)
return False
if tripleNumCount == 2 and phoneNum[-4:].isnumeric():
if numbers == 11:
if phoneNum.startswith('1'):
return True
else:
return False
return True
elif tripleNumCount < 2 and phoneNum[count:count + 3].isnumeric():
tripleNumCount += 1
return False
if __name__ == '__main__':
main()