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 7 Years Ago by Kruptein: n/a

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'

Or what about using chr() and then just adding 65?:

>>> 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 7 Years Ago 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 7 Years Ago by bumsfeld: n/a

This question has already been answered. Start a new discussion instead.