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
TypeError: list indices must be integers, not tuple

``````def numtolet(num):
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']
nummers = []
for i in range(1,25):
nummers.append(i)
pos = nummers.find(num)
poss = letters.find(pos - 1)
return poss``````

Edited by Kruptein: n/a

4
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by bumsfeld

You mean something like this?

``````def numtolet(num):
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']
return letters[num]``````

>>> numtolet(1)
'B'
>>> numtolet(14)
'O'

``````>>> def numtolet(num):
...     return chr(num + 65)
...
>>> numtolet(1)
'B'
>>> numtolet(12)
'M'
>>> numtolet(14)
'O'
>>>``````

Or the reverse:

``````>>> def lettonum(let):
...     return ord(let) - 65
...
>>> lettonum('O')
14
>>> lettonum('A')
0
>>> lettonum('N')
13
>>>``````

Edited by jlm699: n/a

This will also trap number exceeding the index:

``````import string

def numtolet(num):
if num > 25: return
return string.ascii_uppercase[num]

print( numtolet(0) )   # A
print( numtolet(5) )   # F
print( numtolet(25) )  # Z
print( numtolet(26) )  # None``````

Edited by bumsfeld: n/a