| | |
Alphabetic Character Translator To Numeric
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
Well there are a few ways to do it. Preferably i would use a dictionary but as we cant here is another way:
an example output:
Wow.. that worked better then i thought it would.
Enjoy!
python Syntax (Toggle Plain Text)
import string number = raw_input("Please enter the number:").lower() n_list = list(number) for enu,f in enumerate(number): if f in string.ascii_lowercase: i = string.ascii_lowercase.index(f)+2 i /= 3 if i == 0: i +=1 n_list[enu] = '123456789'[i] print ''.join(n_list)
•
•
•
•
Please enter the number:1800-testing
1800-8378463
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
Check out my Site | and join us on IRC | Python Specific IRC
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
Not to be picky, but string method isalpha() is preferable by some. This will avoid the string module:
Example:
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.
Python Syntax (Toggle Plain Text)
def phone_alpha_number(phone_num): nums = '22233344455566677778889999' alphas = 'abcdefghijklmnopqrstuvwxyz' return ''.join([[s, nums[alphas.find(s)]][s.isalpha() or 0] for s in phone_num])
Example:
Python Syntax (Toggle Plain Text)
>>> phone_alpha_number('(555)not-best') '(555)668-2378' >>>
•
•
Join Date: Dec 2006
Posts: 1,056
Reputation:
Solved Threads: 297
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.
•
•
Join Date: Dec 2008
Posts: 2
Reputation:
Solved Threads: 0
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
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
![]() |
Other Threads in the Python Forum
- Previous Thread: ImportError: no module named main?
- Next Thread: random.randint help
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti approximation assignment avogadro backend basic beginner binary bluetooth calculator character cmd code customdialog decimals dictionaries dictionary drive dynamic error examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime program programming progressbar projects py2exe pygame pyqt python random recursion recursive refresh scrolledtext sqlite ssh statistics stdout string strings sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable windows write wxpython xlib






