IN PYTHON! I got an answer but it used a list instead of a dictionary, how would
ID: 3684146 • Letter: I
Question
IN PYTHON!
I got an answer but it used a list instead of a dictionary, how would I modifify it so that it uses a dictionary object instead of list objects.
ex: d = {'a': '2', 'b': '2', 'c': '2',
'd': '3', 'e': '3', 'f': '3',
'g': '4', 'h': '4', 'i': '4',
'j': '5', 'k': '5', 'l': '5',
'm': '6', 'n': '6', 'o': '6',
'p': '7', 'q': '7', 'r': '7', 's': '7',
't': '8','u': '8', 'v': '8',
'w': '9', 'x': '9', 'y': '9', 'z': '9'}
instead of:
d=["abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"]
here is the code, I'm not allowed to import any library functions:
d=["abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"]
while(True):
s = input("ENTER a telephone NUMBER:-")
l = len(s)#calculating length of the string
if(l==0):#if it is a null string then loop breaks...
break
s.lower()#converting alphabets to lower characters...
c=0
for i in range(l):
if(s[i].isalnum()):
c=c+1#calculating the length of the given number
ii=0#variable to check how '-' are needed to print between numbers..
t=0
if(c==10 or c==7):#checking whether valid number or not....if valid then
for i in range(l):
if(s[i].isalnum()):#checking alphanumberic or not..to check whether to print or not..
if(s[i].isalpha()):#if it is alpha then needed to convert to numberic..
for j in range(8):
if (s[i].lower() in d[j]):
print (j+2,end='')#printing number..
ii=ii+1
break
else :
print (s[i],end='')#printing number..
ii=ii+1
if(ii==3 and t<2):#checking how '-' are needed to print between numbers..
if(c==7):
if(t<1):
print('-',end='')
else:
print('-',end='')
ii=0
t=t+1
else :
print ("Invalid number!")#if it is not a valid number....
print (" ")
How do I fix the above code so that it displays the following results:
>>> getNumber()
Enter a telephone number: 612.got.milk
Numeric telephone number is: 612-468-6455
Enter a telephone number: 612.not.milky
Invalid number!
Enter a telephone number: Voo doo Dude
Numeric telephone number is: 866-366-3833
Enter a telephone number: got#milk
Numeric telephone number is: 468-6455 Enter a telephone number:
Explanation / Answer
## Reverse Phone Spelling Program
## Juhwan Park (3917664)
dictionary = {'a': '2', 'b': '2', 'c': '2',
'd': '3', 'e': '3', 'f': '3',
'g': '4', 'h': '4', 'i': '4',
'j': '5', 'k': '5', 'l': '5',
'm': '6', 'n': '6', 'o': '6',
'p': '7', 'q': '7', 'r': '7', 's': '7',
't':'8', 'u': '8', 'w': '9', 'v': '8',
'w': '9', 'x': '9', 'y': '9', 'z': '9'}
done = False
while not done:
number = input("Enter a telephone number: ").lower()
for i in number:
if i in " .,)(#$%^&*-_+<>'"'"'"{}[]=?/!@;:":
number = number.replace(i,"")
if number == '':
done = True
else:
if len(number) != 7 and len(number) != 10:
print("Invalid Number!")
else:
trans = ''
if len(number) == 7:
for i in number:
if i not in dictionary.keys():
trans += i
if len(trans) == 3:
trans += '-'
else:
trans += dictionary[i]
if len(trans) == 3:
trans += '-'
elif len(number) == 10:
for i in number:
if i not in dictionary.keys():
trans += i
if len(trans) == 3:
trans += '-'
elif len(trans) == 7:
trans += '-'
else:
trans += dictionary[i]
if len(trans) == 3:
trans += '-'
elif len(trans) == 7:
trans += '-'
print("Numeric telephone number is: %s" %trans)