Search code examples
pythonasciiemoji

My translator does not output what i want it to


I need my code to output 'hey 😀' when the input written is 'hey :)'. Instead it outputs 'h'

def emojiChanger(word):
    emoji = " "
    for letter in word:
        if letter in ":)":
            emoji = emoji + "😀"
        elif letter in ":(":
            emoji = emoji + "☚ī¸"
        else:
            emoji = emoji + letter
        return emoji

print(emojiChanger(input('How are you doing? ')))

output:

How are you doing? :)
 😀

alternative output: # What I need to fix

How are you doing? hey :)
 h

I need it to output: hey 😀


Solution

  • It can only go into one of the if or elif or else branches (and it can never go into elif for : because the if already captures that). You need to add stuff to the output string in all of these cases, probably outside the conditional.

    However, you are examining one letter at a time, so : followed by anything will add 😀 and ) anywhere else in the line will do that too; and ( anywhere will trigger ☚ī¸

    A better approach altogether is perhaps

    def emojiChanger(word):
        return word.replace(':)', '😀').replace(':(', '☚ī¸')