Python – Cell Phone Number Pad Input


Here is the first version of a little python program I made that will translate input from a cellphone text pad or a number pad to text.
UPDATE: VERSION 2
It’s pretty darn simple.

It would go perfect with this:
http://www.flickr.com/photos/svofski/3383950702/in/pool-make

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import string as st
nar = []
 
nar.append([' '])
nar.append(['!','@','#','$','%','^','&','*','(',')'])
nar.append(['a','b','c'])
nar.append(['d','e','f'])
nar.append(['g','h','i'])
nar.append(['j','k','l'])
nar.append(['m','n','o'])
nar.append(['p','q','r','s'])
nar.append(['t','u','v'])
nar.append(['w','x','y','z'])
 
bigres = ""
while 1:
        inp = raw_input("")
        #print inp
        inp0 = int(inp[0])
        #print inp0
        inpc = st.count(inp,inp[0])
        inpcmod = inpc % len(nar[inp0])
        inpc = inpcmod -1
        #print inpc
        inpResult = nar[inp0][inpc]
        print bigres + " + " + inpResult
        bigres += inpResult
Share and Enjoy:
  • Digg
  • StumbleUpon
  • Facebook
  • Twitter
  • Google Bookmarks
  • Add to favorites
  • email
  • Google Buzz
  • Reddit
  1. #1 by Dallas on August 16, 2008 - 10:13 pm

    Hey you mentioned that it would convert from cell pad or number pad. Keep in mind that those two aren’t the same (they’re inverted forms of one another).

    Also, happy birthday!

  2. #2 by Rami Davis on March 4, 2009 - 9:27 pm

    A few friendly pointers:
    * Variable names are not clear. Nothing in the code is clear.
    * The library “string” is deprecated, instead, strings now have their methods included.
    * Since the sizes of the inside elements of the nar array are static, it should be more clever to use tuples ().
    * The program doesn’t catch errors when converting input to integers (# and *).
    Also, by adding ‘ #!/usr/bin/env python ‘ as the first line, this can be run as a bash script, without needing to do ‘python filename’ and just do ‘./ filename’ instead.

  3. #4 by Daniel on March 5, 2009 - 1:29 pm

    Hey Rami,

    I see your post on Ubuntu Forums. I hope you’re able to get this little piece of code working better. I had the same sort of idea for using a numberpad like a keyboard input. I was thinking that you could mount it on the outside of a messenger bag or something.

    If you make any headway or cleanup my code, please message me back on this site and I can update the post with credits to you.

    Let me know if you have any questions, thanks.

    Daniel

  4. #5 by Rami Davis on March 5, 2009 - 11:16 pm

    I just shot you a new version via email.

(will not be published)