Phone numbers, Python Programming Phone numbers in the United States consist of
ID: 3855703 • Letter: P
Question
Phone numbers, Python Programming
Phone numbers in the United States consist of 10 digits, sometimes formatted with punctuation and spaces. Write a program that prompts for a phone number and determines whether or not it is a valid 10-digit number by ignoring any punctuation. Do this by writing a function that takes the phone number string as a parameter and returns True if it is valid, False if not. (Hint: use a for loop to iterate over the string and increment a counter whenever you see a digit.)
Explanation / Answer
def main():
p_no=input(' enter the phone number xxx-xxx-xxxx : ')
validNumber(p_no)
translate number(p_no)
def validNumber(p_no):
if len(p_no) != 12:
return false
fir i in range(12):
if i in [3,7]:
if p_no[i] != '-':
return false
elif not p_no[i].isalnum():
return false
return true
def translateNumber(p_no):
s=" "
for char in p_no:
if char is '1':
x1 ='1'
s=s+x1
elif char is '-':
x2='-'
s=s+x2
elif char in 'ABCabc':
x3='2'
s=s+x3
elif char in 'DEFdef':
x4='3'
s=s+x4
elif char in'GHIghi':
x5='4'
s=s+x5
elif char in 'JKLjkl':
x6='5'
s=s+x6
elif char in 'MNOmno':
x7='6'
s=s+x7
elif char in 'PQRSpqrs':
x8='7'
s=s+x8
elif char in 'TUVtuv':
x9='8'
s=s+x9
elif char in 'WXYZwxyz':
x10='9'
s=s+x10
print (s)