Python foreign language translator program


I am trying to write a basic python program that translates English words to French words.

The English and French words are stored in a dictionary data structure. The English words are the keys and the French words are the corresponding values.

I wrote a function that takes an English word as an argument and returns either a translation of the English word with its corresponding French word, if it is in the dictionary, or a phrase saying that the English word is not in the dictionary. I am having trouble getting the program to work properly. Here is my code:

english_to_french = {“house”:“maison”, “apple”:“pomme”, “tree”:“arbre”, “horse”:“cheval”, “yellow”:“jaune”, “keyboard”:“clavier”, “water”:“eau”, “start”:“commencer”, “city”:“ville”,“ear”:“oreille”, “eel”:“anguille”, “tooth”:“dent”}

def translate(eng):
for key in english_to_french.iterkeys():
if key == eng:
print english_to_french[key]
print "Sorry that word doesn’t exist in French!"

I will take another look at this when I have my PC on later (iOS now). What version of Python are you using and what type of output or error is the program producing?
At first glance it seems that you could replace iterkeys() with keys() and you need a variable to hold the associated value.
Based on the print statement you must be using Python 2.x so maybe you do need iterkeys…

Your control flow is wrong. The code will always break on the first iteration of the loop. Change the code so that you iterate through the whole loop.

# current logic
  if key == eng:
Loop through keys
  check if key equals eng
      Yes, print to console
       exit function
Key not found
print to screen
exit function

I am using Python 2.7.12 and I when I pass the function my English word, it always returns the phrase that the word doesn’t exist, even if the English word is in the dictionary.

I just tested your code and works perfectly. When you call the function did you use quotes around the word?


Let’s try to make what is already there work. I know the editor removed the indentation which is important in Python. Try these 3 steps…

  1. Move your second print statement to the last line in your function, so it is at the same level of indentation as your For loop.
  2. In your if statement, change break to return
  3. In your else statement, change break to continue.
    That should fix it.
    Reggie01 had it right, you were not getting past the first iteration of the for loop.
    The changes will allow the function to test the first key, if it is not true it will continue with the next key. If no match is found, the interpreter will execute the next line of code (at the same level of indentation) after the for loop.
    If a match is found, break would only exit the loop, and it would print the value followed by the “does not exist” message, so when a match is found you want to exit the function. That’s where return comes in.
1 Like


Instead of iterating all the values of your dict, I wonder why you don’t simply use the dict’s object methods.

dico = {"house":"maison", "apple":"pomme", "tree":"arbre", "horse":"cheval", "yellow":"jaune", "keyboard":"clavier", "water":"eau", "start":"commencer", "city":"ville","ear":"oreille", "eel":"anguille", "tooth":"dent"}

def traduire(key):
    if dico.has_key(key):
        print dico.get(key)
        print "Sorry that word doesn't exist in French!"

# return 'cheval'
1 Like