Python Forum

Full Version: Emoji Dictionary Help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

So I am trying to replace words from the sentence input by the relevant emoji from this dictionary. In the end i would like to reprint the sentence containing the replaced emojis.
Below is the code I've attempted to write so far, but I cannot get the complete sentence to print properly.
emoji_dict = {
    "happy": "😃",
    "heart": "😍",
    "rotfl": "🤣",
    "smile": "😊",
    "crying": "😭",
    "kiss": "😘",
    "clap": "👏",
    "grin": "😁",
    "fire": "🔥",
    "broken": "💔",
    "think": "🤔",
    "excited": "🤩",
    "boring": "🙄",
    "winking": "😉",
    "ok": "👌",
    "hug": "🤗",
    "cool": "😎",
    "angry": "😠",
    "python": "🐍",
}

sentence = input("Please enter a sentence: ")
words = sentence.split()

print(words)

emoji_words = ("")

for word in words:
    if word in emoji_dict:
        word = emoji_dict[word]
    emoji_words += word

print(emoji_words)
Please post code in propper tags. Makes it easier to read.
Maybe something like this

words = input('Type something: ').split()
emoji = {'smile': ':)', 'wink': ';)'}

chars = ['.', '!', '?']
for i, word in enumerate(words):
    if word[-1:] in chars and word[:-1] in emoji.keys():
        words[i] = emoji[word[:-1]] + word[-1:]
    else:
        if word in emoji.keys():
            words[i] = emoji[word]

print(' '.join(words))
Output:
Type something: have a smile and a wink. have a :) and a ;).
Line 33 change to:
emoji_words += f'{word} '
Or use menator01 code.
(May-07-2022, 10:17 AM)snippsat Wrote: [ -> ]Line 33 change to:
emoji_words += f'{word} '
Or use menator01 code.

Thank you, but this gives syntax error.
(May-07-2022, 01:13 PM)rewainy Wrote: [ -> ]Thank you, but this gives syntax error.
Then i guess you use a Python version less than Python 3.6(f-string was new in this release)
Try change to this:
emoji_words += '{} '.format(word)
You should definitely use newer Python version.
A loot will not work if use old version of Python.
(May-07-2022, 01:46 PM)snippsat Wrote: [ -> ]
(May-07-2022, 01:13 PM)rewainy Wrote: [ -> ]Thank you, but this gives syntax error.
Then i guess you use a Python version less than Python 3.6(f-string was new in this release)
Try change to this:
emoji_words += '{} '.format(word)
You should definitely use newer Python version.
A loot will not work if use old version of Python.

I am sorry, it did work now actually...both methods you sent.

Thank you for your support :)