943,808 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 772
  • Python RSS
Dec 5th, 2008
0

Alphabetic Character Translator To Numeric

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jcp200817 is offline Offline
2 posts
since Dec 2008
Dec 6th, 2008
0

Re: Alphabetic Character Translator To Numeric

Well there are a few ways to do it. Preferably i would use a dictionary but as we cant here is another way:
python Syntax (Toggle Plain Text)
  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:
Quote ...
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.
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Dec 6th, 2008
0

Re: Alphabetic Character Translator To Numeric

Not to be picky, but string method isalpha() is preferable by some. This will avoid the string module:
Python Syntax (Toggle Plain Text)
  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:
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Dec 6th, 2008
0

Re: Alphabetic Character Translator To Numeric

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.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Dec 6th, 2008
0

Re: Alphabetic Character Translator To Numeric

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jcp200817 is offline Offline
2 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: ImportError: no module named main?
Next Thread in Python Forum Timeline: random.randint help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC