My code is
def search(n):
lis = []
pad = {1: ['.', ',''?', '!', ':'], 2: ['A', 'B', 'C'], 3: ['D', 'E', 'F'], 4: ['G', 'H', 'I'], 5: ['J', 'K', 'L']
, 6: ['M', 'N', 'O'], 7: ['P', 'Q', 'R', 'S'], 8: ['T', 'U', 'V'], 9: ['W', 'X', 'Y', 'Z'], 0: ' '}
for i in n:
for key,values in pad.items():
if i.upper() in values:
lis.append(key)
return lis
def main():
m = input("Enter String")
print(search(m))
main()
my output is
Enter String - hello world
[4, 3, 5, 5, 6, 0, 9, 6, 7, 5, 3]
It gives output as the single key only. However I want output as [44,33,555,555,666,9,666,777,555,3]
>Solution :
This is because you after you find the key, you only append the key once.
You will need to add the logic to duplicate the key string several times.
I only added one line and modified one line of your original code. Hope this helps.
def search(n):
lis = []
pad = {1: ['.', ',''?', '!', ':'], 2: ['A', 'B', 'C'], 3: ['D', 'E', 'F'], 4: ['G', 'H', 'I'], 5: ['J', 'K', 'L']
, 6: ['M', 'N', 'O'], 7: ['P', 'Q', 'R', 'S'], 8: ['T', 'U', 'V'], 9: ['W', 'X', 'Y', 'Z'], 0: ' '}
for i in n:
for key, values in pad.items():
if i.upper() in values:
temp = str(key) * (values.index(i.upper()) + 1) // added
lis.append(temp) // modified
return lis
def main():
m = input("Enter String")
print(search(m))
main()