Python Forum
Trying to code backwords in if statement for different output in some scenarios
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to code backwords in if statement for different output in some scenarios
#1
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.
Reply
#2
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'))
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a snippet code akbarza 2 373 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  ttp template not working when values are absent in some scenarios jss 0 376 Oct-31-2023, 09:01 PM
Last Post: jss
Photo Python code: While loop with if statement HAMOUDA 1 578 Sep-18-2023, 11:18 AM
Last Post: deanhystad
Question Common understanding of output processing with conditional statement neail 6 887 Sep-17-2023, 03:58 PM
Last Post: neail
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 720 Sep-05-2023, 12:50 PM
Last Post: ToniE
  code won't advance to next statement MCL169 2 757 Apr-11-2023, 09:44 PM
Last Post: Larz60+
  I cannot able to see output of this code ted 1 756 Feb-22-2023, 09:43 PM
Last Post: deanhystad
  why I dont get any output from this code William369 2 1,130 Jun-23-2022, 09:18 PM
Last Post: William369
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,673 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  How can I organize my code according to output that I want ilknurg 1 1,180 Mar-11-2022, 09:24 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020