-1

This code gives the alphabet position of the character i want a code that i give the position and returns the alphabet

from string import ascii_lowercase
LETTERS = {letter: str(index) for index, letter in enumerate(ascii_lowercase, start=1)} 

def alphabet_position(text):
    text = text.lower()
    numbers = [LETTERS[character] for character in text if character in LETTERS]
    return ' '.join(numbers)
  • 1
    Does this answer your question? [Convert numbers into corresponding letter using Python](https://stackoverflow.com/questions/23199733/convert-numbers-into-corresponding-letter-using-python) – havingaball Nov 12 '21 at 13:52

2 Answers2

0

Like @havingaball said, it is easier to do with Pythons base functionality. If you really want to use the LETTERS dictionary then you can solve it as answered here. Which would give you the following code:

from string import ascii_lowercase
LETTERS = {letter: str(index) for index, letter in enumerate(ascii_lowercase, start=1)} 

def alphabet_letter(text):
    text = text.lower()
    letters = [list(LETTERS.keys())[list(LETTERS.values()).index(character)] for character in text if character in LETTERS]
    return ' '.join(letters)
-1

This is answered here. You can use some built in Python base functionality to get this to work, you don't really need to build an elaborate function yourself (although it may be good practice!).

havingaball
  • 378
  • 2
  • 11