-1
word = input("Translate a word: ")
for char in word:
    if char in "BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz":
        print(char + "o" + char)
    else:
        print(char)

I have this code for a translator to a language where you add an "o" after each consonant followed by that same consonant again. When i run it and type in for example "stair" it would print out:

sos
tot
a
i
ror

If someone has an idea on how to print this out on the same line without spacing i would be very grateful!

Chandella07
  • 2,089
  • 14
  • 22
  • see: https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space – user2554863 Oct 26 '18 at 14:35
  • 3
    Possible duplicate of [How to print without newline or space?](https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space) – khelwood Oct 26 '18 at 14:35
  • 1
    `print('something',end='')` – mad_ Oct 26 '18 at 14:37
  • Looks like a children's play language I once read about in an Astrid Lindren book :) – Alfe Oct 26 '18 at 14:40
  • 1
    While the question you asked is already answered as duplicate, I think you really need to define a translate function. Then, assuming it's Lindren, your print statement would be: print(translate_to_rovarspraket(word)) – Kenny Ostrom Oct 26 '18 at 14:49

5 Answers5

2

Don't print each time but append the result to a string and print the final string:

word = input("Translate a word: ")
result = ''
for char in word:
    if char not in "aeiouyAEIOUY":
        result+=char + "o" + char
    else:
        result+=char
print(result)
Tony Pellerin
  • 231
  • 1
  • 7
  • I like this solution than others but as pointed out by @KennyOstrom there is nothing wrong with your approach it is just adhering to the best practices and keep the related logic encapsulating in a single function. It makes no sense to submit a new answer just with the same logic and an extra function defined. – mad_ Oct 26 '18 at 14:57
  • I suggested an improvement to this answer. I could propose an edit, but instead I deleted my comment and commented directly to OP. – Kenny Ostrom Oct 26 '18 at 15:03
1

One option is to use list comprehension to create a list with your values:

[char+'o'+char if char in "BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz" else char for char in word]

and then if you need to smash them together into a string you just use a join()

''.join([char+'o'+char if char in "BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz" else char for char in word])
dfundako
  • 8,022
  • 3
  • 18
  • 34
  • while it works, your answer is way more complicated than it has to be, the OP could just print without a newline at the end. – Azrael Oct 26 '18 at 14:37
  • Why promoting list comprehension when the answer does not necessarily need it? Seriously for printing in new line you have to create a new list?? – mad_ Oct 26 '18 at 14:45
0

Add records to a list and use print function with * to print records in the same line.

word = input("Translate a word: ")
data = []  #create a empty list
for char in word:
    if char in "BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz":
        data.append(char + "o" + char)  #add elements to list
    else:
        data.append(char)  #add elements to list

print(*data, sep='')  #print list elements in single line without spaces
Chandella07
  • 2,089
  • 14
  • 22
0

If you want to use the print statement as you are currently doing, just try below option of print statement which defines the ending of the print statement.

word = input("Translate a word: ")
for char in word:
    if char in "BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz":
        print(char + "o" + char,end="")
    else:
       print(char,end="")

The output of the above code on input "stair" will be "sostotairor". Hope it helps!

-1

You can double the consonants in a list first and join the characters with o:

word = input("Translate a word: ")
for char in word.lower():
    print('o'.join([char] * ((char not in 'aeiou') + 1)))

Sample input/output:

Translate a word: stair
sos
tot
a
i
ror
blhsing
  • 91,368
  • 6
  • 71
  • 106