Alphabetic Character Translator To Numeric

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2008
Posts: 2
Reputation: jcp200817 is an unknown quantity at this point 
Solved Threads: 0
jcp200817 jcp200817 is offline Offline
Newbie Poster

Alphabetic Character Translator To Numeric

 
0
  #1
Dec 5th, 2008
Hey,
I have to do a script for class in which a user inputs a phone number like the ones you see on TV. 555-buy-this I need to make a script that will change the alphabetic characters to numbers. I asked my teacher and he said we had to do it using a list. I am totally lost and would appreciate any help.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 944
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 146
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Alphabetic Character Translator To Numeric

 
0
  #2
Dec 6th, 2008
Well there are a few ways to do it. Preferably i would use a dictionary but as we cant here is another way:
  1. import string
  2. number = raw_input("Please enter the number:").lower()
  3. n_list = list(number)
  4.  
  5. for enu,f in enumerate(number):
  6. if f in string.ascii_lowercase:
  7. i = string.ascii_lowercase.index(f)+2
  8. i /= 3
  9. if i == 0:
  10. i +=1
  11.  
  12.  
  13. n_list[enu] = '123456789'[i]
  14.  
  15. print ''.join(n_list)
an example output:
Please enter the number:1800-testing
1800-8378463
Wow.. that worked better then i thought it would.
Enjoy!
Last edited by Paul Thompson; Dec 6th, 2008 at 12:20 am.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: Alphabetic Character Translator To Numeric

 
0
  #3
Dec 6th, 2008
Not to be picky, but string method isalpha() is preferable by some. This will avoid the string module:
  1. def phone_alpha_number(phone_num):
  2. nums = '22233344455566677778889999'
  3. alphas = 'abcdefghijklmnopqrstuvwxyz'
  4. return ''.join([[s, nums[alphas.find(s)]][s.isalpha() or 0] for s in phone_num])

Example:
  1. >>> phone_alpha_number('(555)not-best')
  2. '(555)668-2378'
  3. >>>
I know it's a ridiculous use of a list comprehension, but I would not expect the OP to turn this in for his homework.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,056
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 297
woooee woooee is online now Online
Veteran Poster

Re: Alphabetic Character Translator To Numeric

 
0
  #4
Dec 6th, 2008
As has already been hinted, you are probably expected to use 2 lists. The first contains letters and the second contains the number for the corresponding letter. You could also use a list of lists, with each sub-list containing the letter and number, but that is probably more than you can wrap your head around at this point. Do whatever makes sense to you. There are only 26 letters in the English alphabet, so the list is very small, which means that any method with take about the same amount of time. Code something, and append it to the thread for additional help.
Last edited by woooee; Dec 6th, 2008 at 12:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 2
Reputation: jcp200817 is an unknown quantity at this point 
Solved Threads: 0
jcp200817 jcp200817 is offline Offline
Newbie Poster

Re: Alphabetic Character Translator To Numeric

 
0
  #5
Dec 6th, 2008
paulthom12345 --I tried your way and I couldn't get it to work. Some of the letters could turn to numbers but now all of them. I don't know why it would change some letters and not all of them.

The suggestion about using a dictionary is a good one, I had not thought of it myself. I might try it that way and if the teacher dont like it well, tough luck for him
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC