How can I iterate through 3 dictionaries for a translator?

99 Views Asked by At

Im working on a translator (english to braille) as a project for a class in python 2.7. We've been essentially thrown to the wolves and I've never had experience coding before. How in the hell am I supposed to create a function that iterates through one dictionary and spits out the value for each letter in english(only one dictionary so I get the general idea)?

Ive got a comfortable 3 dictionaries for each row that a braille letter takes to make, and im honestly just not sure where to start in terms of the function. Ive got a prompt asking for a word, but idk how to make python look at individual letters in a word and reference those letters to a dictionary.

eng_to_braille_1 = {
  'a': '. ', 'b': '. ', 'c': '..', 'd': '..', 'e': '. ', 'f': '..', 'g': '..', 'h': '. ', 'i': ' .', 'j': ' .', 'k': '. ', 'l': '. ', 'm': '..', 'n': '..', 'o': '. ', 'p': '..', 'q': '..', 'r': '. ', 's': ' .','t': ' .', 'u': '. ', 'v': '. ', 'x': '..', 'y': '..', 'z': '. '
}

eng_to_braille_2 = {
  'a': '  ', 'b': '. ', 'c': '  ', 'd': ' .', 'e': ' .', 'f': '. ', 'g': '..', 'h': '..', 'i': '. ', 'j': '..', 'k': '  ', 'l': '. ', 'm': '  ', 'n': ' .', 'o': ' .', 'p': '. ', 'q': '..', 'r': '..', 's': '. ','t': '..', 'u': '  ', 'v': '. ', 'x': '  ', 'y': ' .', 'z': ' .'
}

eng_to_braille_3 = {
  'a': '  ', 'b': '  ', 'c': '  ', 'd': '  ', 'e': '  ', 'f': '  ', 'g': '  ', 'h': '  ', 'i': '  ', 'j': '  ', 'k': '. ', 'l': '. ', 'm': '. ', 'n': '. ', 'o': '. ', 'p': '. ', 'q': '. ', 'r': '. ', 's': '. ','t': '. ', 'u': '..', 'v': '..', 'x': '..', 'y': '..', 'z': '..'
}

word = input("Type a word to be translated: ")
word = str()

def translate(word):
   translation = ""
2

There are 2 best solutions below

2
On

You can define the inside function like this:

for translator in [eng_to_braille_1, eng_to_braille_2, eng_to_braille_3]:
    for i in word:
        print(translator[i], end=' ')
    print('\n')

The top row, middle & bottom row will be printed. But this cant be done for long string input. For that, you better find unicode for braille.

0
On

Python dictionary is key, value storage. So if you want to find the braille value for 'a' in eng_to_braille_1 then do the following:

print(eng_to_braille_1['a'])

For a word you have to iterate over each letter in the word and find its equivalent braille value.

for c in word:
    print(eng_to_braille_1[c])