Text to key number in python

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 = {
'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]):

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...

Leave a Reply

Your email address will not be published. Required fields are marked *