Python Forum
Output discrepancy when building Translator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Output discrepancy when building Translator
#1
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.
Reply
#2
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.
Reply
#3
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 лб
Reply
#4
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.
Reply
#5
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 + "и"
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
I already answered this question here: https://python-forum.io/Thread-Output-di...5#pid62995
why are you posting a duplicate question?
Reply
#7
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
Reply
#8
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 + "и"
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
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: ")))
Reply
#10
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".
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  discrepancy with datetime with timezone XJia 3 713 Sep-03-2023, 02:58 PM
Last Post: deanhystad
  (python) Can i get some help fixing a English to Morse translator? Pls AlexPython 7 1,540 Sep-12-2022, 02:55 AM
Last Post: AlexPython
  i want to write symbole as varibales in python to make translator rachidel07 9 3,410 Feb-03-2021, 09:57 PM
Last Post: nilamo
  letter translator (or someting) Obsilion 4 2,421 Apr-28-2020, 12:40 PM
Last Post: deanhystad
  I want to filter out words with one letter in a string (pig latin translator) po0te 1 2,065 Jan-08-2020, 08:02 AM
Last Post: perfringo
  Trouble with Regex Translator skrivver99 3 2,718 Dec-15-2018, 03:55 PM
Last Post: Gribouillis
  Help with pig latin translator DragonG 1 2,239 Nov-01-2018, 03:57 AM
Last Post: ichabod801
  PigLatin Sentence Translator Help 2skywalkers 7 5,750 Jun-23-2018, 12:46 AM
Last Post: 2skywalkers
  Pig Latin Translator RickyWilson 1 3,157 Dec-02-2017, 08:20 AM
Last Post: buran

Forum Jump:

User Panel Messages

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