Mission: Emoji Translator 3000 ๐ค
Final build of the Function Factory: a machine that turns words into emoji.
You'll combine functions, return, and the dictionary you learned in Unit 4. Here's the idea:
emoji_book = {"cat": "๐ฑ", "pizza": "๐"}
def translate(word):
if word in emoji_book:
return emoji_book[word]
return "๐คท never heard of it"
print(translate("cat")) # โ ๐ฑ
print(translate("banana")) # โ ๐คท never heard of it
That last return is the default โ it catches every word your
translator doesn't know, so it never hands back nothing.
(You could use if / elif / else instead of a dictionary. Both work!
The dictionary is way less typing once you know a lot of words.)
Your mission
- Build a function
translate(word)that returns an emoji - Teach it at least 4 words (a dictionary, or a stack of
elifs) - Give it a default answer for words it doesn't know
- Call
translateat least 3 times and print each result
No input() for this one โ just call translate("cat") with words you
pick yourself, so the machine can be tested. ๐ค
Your code
Press โถ Run to see what your code does!