0

I have looked up this and not found anything usefule So I want to take a unknown number (1-26) and convert it to its corrosponding letter.

Example:

import random
rand_let = random.randint(1,26)
print(rand_let)
#Insert the numb letter converter here
print(rand_let) # This is the changed version
output: 
3
C

3 Answers3

5

chr() is your converter function. There is an offset of 64, as the numbers to be used are from the ASCII table of characters (i.e.: A = 65, B = 66, ...):

# import random
# rand_let = random.randint(1,26)
# print(rand_let)
rand_let = chr(rand_let + 64)
# print(rand_let) # This is the changed version
SpghttCd
  • 10,510
  • 2
  • 20
  • 25
0

One way you could solve this problem is by making a bunch of if thens like so:

if rand_let == 1:
    print("A")

Another simple way is to make a list of letters and then ask for the letter with a rand_let - 1 index (because the 1st thing in a list has an index of 0.

LetterList = ["A", "B", "C", "D", "E", "F", and so on...]
letter = LetterList[rand_let-1]
print(letter)
MakeHellTal
  • 606
  • 5
  • 11
0

There are a couple ways to do this. First you can create a dictionary and assign letters as numbers. another way would be to find the chr() code number then use a random number generater to pick a char num. This is what I recommend.

# imports
import random
# pick a random number between the ord of 'a' and the ord of 'z' Google these for 
# more info
pick = random.randint(ord('a'), ord('z'))
# print the char of the ord of the random number picked
print(chr(pick))