Python Forum
Output discrepancy when building Translator - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Output discrepancy when building Translator (/thread-14005.html)

Pages: 1 2


Output discrepancy when building Translator - skrivver99 - Nov-11-2018

Hi, I am having an issue trying to build a functioning translator for Python 3.5 in IDLE. I want each letter I input to be converted to a new letter on the other end. Here is the code:
def translate(phrase):
    translation = ""
    for letter in phrase:
        if letter in "Aa":
            translation = translation + "a"
        if letter in "Bb":
            translation = translation + "б"
        if letter in "Cc":
            translation = translation + "к"
        if letter in "Ll":
            translation = translation + "л"
        if letter in "Ii":
            translation = translation + "и"
        else:
            translation = translation + letter
    return translation

print(translate(input("Enter word: ")))
As the user I then type "abc" for example, and get this:

aaбbкc

Am I doing something wrong? Please help.


RE: Output discrepancy when building Translator - stullis - Nov-11-2018

The problem is that you have a series of if statements instead of if...elif...else. So, when your translator reaches the final if and it's false, it adds the original letter to the end. Change the ifs to elifs and it will work.


RE: Output discrepancy when building Translator - Larz60+ - Nov-11-2018

A dictionary would work better for something like this:
tdict = {
	'a': 'a',
	'b': 'б',
	'c': 'к',
	'l': 'л',
	'i': 'и'
}

def translate(phrase):
	translation = ''
	for letter in phrase.lower():
		if letter in tdict:
			translation = f'{translation}{tdict[letter]}'
		else:
			print(f'{letter} is not in dictionary')
	return translation

print(translate('cab'))
print(translate('ilabc'))
print(translate('xlbd')) # try missing letter
results:
Output:
кaб илaбк x is not in dictionary d is not in dictionary лб



Using an "unequal" or "and not in" command in if statement - skrivver99 - Nov-17-2018

Hi, I want to write an if statement which stipulates that if an input is contained within one string, but is contained within another string which contains the first string, the output will be unique and for the second string, rather than the first.

For example, I want the input "ceiling" to make the output "силиng" after printing. Here is the current state of the code I've been messing around with:
def translate(phrase):
    translation = ""
    for letter in phrase:
        if letter in "Aa":
            translation = translation + "a"
        elif letter in "Bb":
            translation = translation + "б"
        elif letter in "Cc" and letter != "cei":
            translation = translation + "к"
        elif letter in "Ll":
            translation = translation + "л"
        elif letter in "Ii":
            translation = translation + "и"
        elif letter in "Ee":
            translation = translation + "е"
        elif letter in "cei":
            translation = translation + "си"
        else:
            translation = translation + letter
    return translation


print(translate(input("Enter word: ")))
Can anyone fix the code I've written so that I can achieve what I want? I hope my intentions are understood too, but if not I will clarify.


RE: Using an "unequal" or "and not in" command in if statement - ichabod801 - Nov-17-2018

As Larz said, please use python and output tags when posting code and results. I put them in for you this time.

I think the problem here is that you are mostly translating one letter, but sometimes you want to translate 3 letters (cei). For example, letter will never equal 'cei', because letter is always a single letter (assuming phrase is a string.

I think the best thing would be to check backwards when you check for I/i:

elif letter in 'Ii' and translation[-2:] == 'кe':
    translation = translation[:-2] + "cи"
elif letter in 'Ii':
    translation = translation + "и"



RE: Using an "unequal" or "and not in" command in if statement - Larz60+ - Nov-17-2018

I already answered this question here: https://python-forum.io/Thread-Output-discrepancy-when-building-Translator?pid=62995#pid62995
why are you posting a duplicate question?


RE: Using an "unequal" or "and not in" command in if statement - skrivver99 - Nov-17-2018

I don't understand your solution. I wrote exactly what you wrote and it is just ignoring that part of the code. The input doesn't change


RE: Output discrepancy when building Translator - ichabod801 - Nov-17-2018

Sorry, my code was using the wrong 'e' in the conditional. Try this:

        elif letter in 'Ii' and translation[-2:] == 'ке':
            translation = translation[:-2] + "cи"
        elif letter in 'Ii':
            translation = translation + "и"



Need help with translator - skrivver99 - Nov-24-2018

Hi, can someone please help me see what is wrong with the following code?

For example, I want the input "ceiling" to produce "силинг".


def translate(phrase):
    translation = ""
    for letter in phrase:
        if letter in "Aa":
            translation = translation + "a"
            # Capitals
        elif letter.lower() in "b":
            if letter.isupper():
                translation = translation + "Б"
            else:
                translation = translation + "б"
        elif letter in "Cc" and translation[+2:] == "ei":
            translation = translation[:+2] + "си"
        elif letter in "Ll":
            translation = translation + "л"
        #elif letter in "Cc":
            #translation = translation + "к"
        #elif letter in 'Ii' and translation[-2:] == "kе":
            #translation = translation[:-2] + "cи"
        elif letter in "Ii":
            translation = translation + "и"
        elif letter in "Ee":
            translation = translation + "е"
        elif letter is "cei":
            translation = translation + "си"
        elif letter in "Ff":
            translation = translation + "ф"
        elif letter in "Dd":
            translation = translation + "д"
        elif letter in "Gg":
            translation = translation + "г"
        else:
            translation = translation + letter
            '''
This list
Needs to be completed
'''
    return translation

print(translate(input("Enter word: ")))



RE: Need help with translator - j.crater - Nov-24-2018

Hello,
in case of "cei" you use "is" instead of "in", as is the case in other elifs
elif letter is "cei":
This cannot work, because for loop iterates over characters, so no character will equal "cei".