Text to key number in python
Question:
So my challenge is this:
On some basic cell phones, text messages can be sent using the numeric keypad. Because each key has multiple letters associated with it, multiple key presses are needed for most letters. Pressing the number once generates the first letter on the key. Pressing the number 2, 3, 4 or 5 times generates the second, third, fourth or fifth character listed for that key.
Key Symbols
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 (space)
Write a program that displays the key presses that must be made to enter a text message read from the user. Construct a dictionary that maps from each letter or symbol to the key presses. Then use the dictionary to generate and display the presses for the user’s message. For example, if the user enters Hello, World! then your program should output 4433555555666110966677755531111. Ensure that your program handles both uppercase and lowercase letters. Ignore any characters that aren’t listed in the table above such as semicolons and brackets.
So far i have:
textLetters = {
'1':['.',',','?','!',':'],
'2':['A' or 'a','B' or 'b','C' or 'c'],
'3':['D' or 'd','E' or 'e','F' or 'f'],
'4':['G' or 'g','H' or 'h','I' or 'i'],
'5':['J' or 'j','K' or 'k','L' or 'l'],
'6':['M' or 'm','N' or 'n','O' or 'o'],
'7':['P' or 'p','Q' or 'q','R' or 'r','S' or 's'],
'8':['T' or 't','U' or 'u','V' or 'v'],
'9':['W' or 'w','X' or 'x','Y' or 'y','Z' or 'z'],
'0':[' ']
}
text = list(input("Please input a text so I can show you how to type it on your phone:"))
for i in text:
for k,v in textLetters.items():
if i in v:
for a in range(v[i]):
print(k)
Without the ‘for a in range(v[i])’, I have got this program to print the number (key) of each character (value) inputted. Now I just need the program to print the key a certain number times according to how deep each value is in each key’s list (for example if I inputted e, it would print ‘3’ but I would want it to print ’33’ because e is depth two in 3’s list so it should print ‘3’ twice). Anybody know how to fix this?
Answers:
nums = {
'a': '2',
'b': '22',
'c': '222',
'd': '3',
'e': '33',
# etc...
}
I followed your code, modified it and it worked. first, remove the OR in your dictionary values and convert your text to lower by using the .lower function (eg text.lower()). then remove the v[I] on the last for loop as it returns a string. replace it with v.index(i) which returns the index of i in v.
try this solution. It worked for me.
def textToKey (string) :
keypad = {
'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' : [' ']
}
txt = string.upper()
result = ""
keys = list(keypad.items())
for i in range(len(txt)):
for j in range(len(keys)):
for k in range(len(keys[j][1])):
if txt[i] == keys[j][1][k]:
result+=((k+1)*keys[j][0])
return result
then just print it all as a string…
>>> print(textToKey("Hello, World!"))
4433555555666110966677755531111
str_1 = input("")
str_1 = str_1.upper()
keypad = {'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 str_1:
for key,letters in keypad.items():
if i in letters:
times = letters.index(i)
print(key*(times+1),end="")
Using dictionary may make the code huge. And it is neither efficient nor necessary to solve this problem. You can use lists to do this in a good approach.
lst = [" ",".,?!:","ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"]
s = input().upper()
s2 = ""
for c in s:
for i in range(len(lst)):
for j in range(len(lst[i])):
if lst[i][j] == c:
s2 += str(i)*(j+1)
print(s2)
So my challenge is this:
On some basic cell phones, text messages can be sent using the numeric keypad. Because each key has multiple letters associated with it, multiple key presses are needed for most letters. Pressing the number once generates the first letter on the key. Pressing the number 2, 3, 4 or 5 times generates the second, third, fourth or fifth character listed for that key.
Key Symbols
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 (space)
Write a program that displays the key presses that must be made to enter a text message read from the user. Construct a dictionary that maps from each letter or symbol to the key presses. Then use the dictionary to generate and display the presses for the user’s message. For example, if the user enters Hello, World! then your program should output 4433555555666110966677755531111. Ensure that your program handles both uppercase and lowercase letters. Ignore any characters that aren’t listed in the table above such as semicolons and brackets.
So far i have:
textLetters = {
'1':['.',',','?','!',':'],
'2':['A' or 'a','B' or 'b','C' or 'c'],
'3':['D' or 'd','E' or 'e','F' or 'f'],
'4':['G' or 'g','H' or 'h','I' or 'i'],
'5':['J' or 'j','K' or 'k','L' or 'l'],
'6':['M' or 'm','N' or 'n','O' or 'o'],
'7':['P' or 'p','Q' or 'q','R' or 'r','S' or 's'],
'8':['T' or 't','U' or 'u','V' or 'v'],
'9':['W' or 'w','X' or 'x','Y' or 'y','Z' or 'z'],
'0':[' ']
}
text = list(input("Please input a text so I can show you how to type it on your phone:"))
for i in text:
for k,v in textLetters.items():
if i in v:
for a in range(v[i]):
print(k)
Without the ‘for a in range(v[i])’, I have got this program to print the number (key) of each character (value) inputted. Now I just need the program to print the key a certain number times according to how deep each value is in each key’s list (for example if I inputted e, it would print ‘3’ but I would want it to print ’33’ because e is depth two in 3’s list so it should print ‘3’ twice). Anybody know how to fix this?
nums = {
'a': '2',
'b': '22',
'c': '222',
'd': '3',
'e': '33',
# etc...
}
I followed your code, modified it and it worked. first, remove the OR in your dictionary values and convert your text to lower by using the .lower function (eg text.lower()). then remove the v[I] on the last for loop as it returns a string. replace it with v.index(i) which returns the index of i in v.
try this solution. It worked for me.
def textToKey (string) :
keypad = {
'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' : [' ']
}
txt = string.upper()
result = ""
keys = list(keypad.items())
for i in range(len(txt)):
for j in range(len(keys)):
for k in range(len(keys[j][1])):
if txt[i] == keys[j][1][k]:
result+=((k+1)*keys[j][0])
return result
then just print it all as a string…
>>> print(textToKey("Hello, World!"))
4433555555666110966677755531111
str_1 = input("")
str_1 = str_1.upper()
keypad = {'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 str_1:
for key,letters in keypad.items():
if i in letters:
times = letters.index(i)
print(key*(times+1),end="")
Using dictionary may make the code huge. And it is neither efficient nor necessary to solve this problem. You can use lists to do this in a good approach.
lst = [" ",".,?!:","ABC","DEF","GHI","JKL","MNO","PQRS","TUV","WXYZ"]
s = input().upper()
s2 = ""
for c in s:
for i in range(len(lst)):
for j in range(len(lst[i])):
if lst[i][j] == c:
s2 += str(i)*(j+1)
print(s2)