python - Text Prints in front of Command Prompt - Stack Overflow

admin2025-05-02  1

My pre-programmed translator works almost perfectly, with one minor but rather ugly issue. The translation text prints like this.

[joey@manjarobox Languages]$ python universal.dict
Enter text to translate: 
he should have given up .
eh dloulhs evah nevig pu. [joey@manjarobox Languages]$ 

That last block has no typos. It's how I pasted it.

eng_to_lang = {
    ".":"\b.",
    "?":"\b?", 
    "!":"\b!", 
    ",":"\b,", 

    "he":"eh",           "He":"Eh",
    "should":"dloulhs",  "Should":"Dlouhs", 
    "have":"evah",       "Have":"Evah", 
    "given":"nevig",     "Given":"Nevig", 
    "up":"pu",           "Up":"Pu", 
}

# UNLESS YOU KNOW WHAT YOU'RE DOING, 
# DO NOT TOUCH ANYTHING FROM THIS POINT ON!

value = input("Enter text to translate: \n")
# string = value.lower()
string = value.split()

for element in range(0, len(string)):
    print(eng_to_lang.get(string[element]), end=" ")

I would also like for my code to recognize punctuation and treat it accordingly, that way I don't have to correct everything once I am able to echo translated text in a file. When I tried it, "he," is treated like a single word.

Anyone got a solution?

My pre-programmed translator works almost perfectly, with one minor but rather ugly issue. The translation text prints like this.

[joey@manjarobox Languages]$ python universal.dict
Enter text to translate: 
he should have given up .
eh dloulhs evah nevig pu. [joey@manjarobox Languages]$ 

That last block has no typos. It's how I pasted it.

eng_to_lang = {
    ".":"\b.",
    "?":"\b?", 
    "!":"\b!", 
    ",":"\b,", 

    "he":"eh",           "He":"Eh",
    "should":"dloulhs",  "Should":"Dlouhs", 
    "have":"evah",       "Have":"Evah", 
    "given":"nevig",     "Given":"Nevig", 
    "up":"pu",           "Up":"Pu", 
}

# UNLESS YOU KNOW WHAT YOU'RE DOING, 
# DO NOT TOUCH ANYTHING FROM THIS POINT ON!

value = input("Enter text to translate: \n")
# string = value.lower()
string = value.split()

for element in range(0, len(string)):
    print(eng_to_lang.get(string[element]), end=" ")

I would also like for my code to recognize punctuation and treat it accordingly, that way I don't have to correct everything once I am able to echo translated text in a file. When I tried it, "he," is treated like a single word.

Anyone got a solution?

Share Improve this question asked Jan 2 at 1:38 user28984840user28984840 11 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The behaviour is caused by the use of end=" " in the print statement.

Normally, a newline is printed at the end of a print statement, but this parameter is telling it to simply print a space instead of a newline. Thus, when the program ends, the command line prints on the same line.

You can 'fix' it by simply adding this at the end of your program:

print()

That will force a newline and will start the command line on the next line.

Alternate approach 1: Construct a string

Instead of printing each word within a loop, an alternate approach would be to construct a string and then print the string at the end:

result = ''

for element in string:
    result += eng_to_lang[element] + ' '

print(result)

Alternate approach 2: Joining a list

This approach stores each word in a list, then joins it together in the end:

word_list = []

for element in string:
    word_list.append(eng_to_lang[element])

print(" ".join(word_list))

or even simply:

word_list = [eng_to_lang[word] for word in value.split()]
print(" ".join(word_list))
转载请注明原文地址:http://anycun.com/QandA/1746137245a92088.html