
Exercise for reference:
Create an English to Portuguese translation program.
The program takes a word from the user as input and translates it using the following dictionary as a vocabulary source. Also, return the message, "We couldn't find that word!" when the user enters a word that is not in the dictionary. Also, make the program non-case-sensitive, meaning that, for example, both earth and Earth should return the translation correctly for that word.
Answer:
d = dict(weather = "clima", earth = "terra", rain = "chuva")
def vocabulary(word):
try:
return d[word]
except KeyError:
return "We couldn't find that word!"
word = input("Enter word: ").lower()
print(vocabulary(word))
Explanation:
As you see, we are converting all the string characters to lowercase as soon as we receive the user's input. Then we pass the lowercase version of the string to the dictionary.