numtolet

Thread Solved

Join Date: Sep 2009
Posts: 72
Reputation: Kruptein is an unknown quantity at this point 
Solved Threads: 5
Kruptein's Avatar
Kruptein Kruptein is offline Offline
Junior Poster in Training

numtolet

 
0
  #1
Oct 19th, 2009
okay I tried to make a reverse of finding the num value of a letter bu it returns in an error :f

Traceback (most recent call last):
File "/home/darragh/Bureaublad/t.py", line 15, in <module>
numtolet('A')
File "/home/darragh/Bureaublad/t.py", line 6, in numtolet
letters['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
TypeError: list indices must be integers, not tuple
  1. def numtolet(num):
  2. letters['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
  3. nummers = []
  4. for i in range(1,25):
  5. nummers.append(i)
  6. pos = nummers.find(num)
  7. poss = letters.find(pos - 1)
  8. return poss
Last edited by Kruptein; Oct 19th, 2009 at 3:28 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 50
Reputation: lukerobi is an unknown quantity at this point 
Solved Threads: 16
lukerobi lukerobi is offline Offline
Junior Poster in Training
 
0
  #2
Oct 19th, 2009
You mean something like this?
  1. def numtolet(num):
  2. letters = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
  3. return letters[num]
>>> numtolet(1)
'B'
>>> numtolet(14)
'O'
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #3
Oct 19th, 2009
Or what about using chr() and then just adding 65?:
  1. >>> def numtolet(num):
  2. ... return chr(num + 65)
  3. ...
  4. >>> numtolet(1)
  5. 'B'
  6. >>> numtolet(12)
  7. 'M'
  8. >>> numtolet(14)
  9. 'O'
  10. >>>

Or the reverse:
  1. >>> def lettonum(let):
  2. ... return ord(let) - 65
  3. ...
  4. >>> lettonum('O')
  5. 14
  6. >>> lettonum('A')
  7. 0
  8. >>> lettonum('N')
  9. 13
  10. >>>
Last edited by jlm699; Oct 19th, 2009 at 4:31 pm.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso
 
0
  #4
Oct 19th, 2009
This will also trap number exceeding the index:
  1. import string
  2.  
  3. def numtolet(num):
  4. if num > 25: return
  5. return string.ascii_uppercase[num]
  6.  
  7. print( numtolet(0) ) # A
  8. print( numtolet(5) ) # F
  9. print( numtolet(25) ) # Z
  10. print( numtolet(26) ) # None
Last edited by bumsfeld; Oct 19th, 2009 at 6:45 pm.
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC