-1

Consider this question.

Write a function translate() that will translate a text into "rövarspråket" (Swedish for "robber's language"). That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun") should return the string "tothohisos isos fofunon".

And my solution is given below:


a=[]
a=raw_input("Enter text which you whant to translate in 'roverspraket':\n").lower()
def translate(a):
    for char in a:
        if char is "a" or char is 'e' or char is 'i' or char is 'o' or char is 'u' or char is ' ':
            print char,
        else:
            print char+'o'+char,
print(translate(a))

Its output is given below:

Enter text which you whant to translate in 'roverspraket':
vinay<--- This is input
vov i non a yoy None

My question is i dont want space between these.... how can i do that?

Thanks in advance.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • 8
    Possible duplicate of [How do I keep Python print from adding newlines or spaces?](http://stackoverflow.com/questions/255147/how-do-i-keep-python-print-from-adding-newlines-or-spaces) – shad0w_wa1k3r Apr 06 '17 at 06:38
  • you can use `end=''` in your print statement, like this `print(char, end = '' )` and `print(char+'o'+char, end = '')` which will give you `vovinonayoy` – akash karothiya Apr 06 '17 at 06:45

2 Answers2

0

I think it's more useful to return the extended string:

def rovarspraket(text):
    """ Translate text into robber-language """
    result = ""
    vowels = "aeiou"
    for char in text:
        result += char
        if char not in vowels:
            result += "o" + char
    return result
rolika
  • 381
  • 1
  • 10
  • or simply (needs `import functools`): `return functools.reduce(lambda c1, c2: c1+c2 if c2 in "aeiou" else c1+c2+"o"+c2, text, "")` – rolika Apr 06 '17 at 08:05
0

To solve your problem I would have translate take an input string and return a modified string. That modified string can then be printed without problems. Here is my solution:

def translate(inp):
    out = ''
    for char in inp:
        if char not in ['a','e','i','o','u',' ']:
          out += char+'o'
        out += char

    return out

inp=input("Enter text which you whant to translate in 'roverspraket':\n").lower()

print(translate(inp))

Note:

  • I have changed raw_input to input. The latter is the Python 3.x standard, since I saw you were using print(...). If you want to use Python 2.x instead, you'll have to retain the raw_input.

  • I have replaced your many ors with a more pythonic equivalent, by checking if char is in a list of characters. The behavior is fully equivalent. Only my version looks clear, and might even be faster?

  • I have avoided a minor piece of code repetition by avoiding the elif. Although it really doesn't matter in this small bit of code, I wanted to point this out because it can be a bigger deal when you start writing more serious programs. See also this TED talk of Linus Torvalds (which is mostly not about programming though...).

Tom de Geus
  • 5,625
  • 2
  • 33
  • 77
  • @VinaySharma. You're welcome. (If the question is sufficiently answered please mark the answer(s) that satisfy your need as answered.) – Tom de Geus Apr 06 '17 at 10:01