Python Forum

Full Version: Trying to code backwords in if statement for different output in some scenarios
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone,

So basically I have a problem building a translator, where I want the input "s" to give me "с" and the input "h" to give me "х" but at the same time I want "sh" to give me "ш".

Previously I was given a command to put into my code so I could make "cei" give me "си". I've tried to use the same technique again, but it's not working.

Here is what I have so far (It might look confusing because some of the Cyrillic and Latin letters look the same, but I made sure everything was typed out correctly):

def translate(phrase):
    translation = ""
    for letter in phrase:
        if letter in "A":
            translation = translation + "а"
        elif letter in "Bb":
            translation = translation + "б"
        elif letter in "Cc":
            translation = translation + "к"
        elif letter in "Ll":
            translation = translation + "л"
        elif letter in 'Ii':
            translation = translation + "и"
        elif letter in "Ee":
            translation = translation + "е"
        elif letter in "Ss":
            translation = translation + "с"
        elif letter in "Hh":
            translation = translation + "х"
        elif letter in 'Ii' and translation[-2:] == 'ке':
            translation = translation[:-2] + "cи"
        elif letter in "Hh" and translation[-1:] == "с":
            translation = translation[:-1] + "ш"
        else:
            translation = translation + letter
    return translation
 
 
print(translate(input("Enter word: ")))
I hope you guys can help. Thanks in advance.
Could do something like this.
language = {
    'base': {
        'b': "б",
        'c': "к",
        'l': "л",
        'i': "и",
        's': "с",
        'h': "х"
    },

    'translation 1': {
        'i': ('c', "w")
    },

    'translation 2': {
        'h': ('ke', "cи")
    }
}

def translate(pharse):
    translation = ""

    for letter in pharse:
        letter = letter.lower()

        key = 'translation 2'
        if letter in language[key]:
            if translation[-2:] == language[key][letter][0]:
                translation += language[key][letter][1]
                continue

        key = 'translation 1'
        if letter in language[key]:
            if translation[-1:] == language[key][letter][0]:
                translation += language[key][letter][1]
                continue

        if letter in language['base']:
            translation += language['base'][letter]
        else:
            translation += letter

    return translation

print(translate('Keh'))
print(translate('s'))