1
message="this is fun"

def translate(robber):
    consonants=['bcdfghjklmnpqrstvwxz']
    for letters in robber:
        if letters in consonants:
            return (letters + 'o' + letters)
            continue
        else:
            return translate

print translate(message)

Trying to make a nested loop that takes a string and translates it into robbers language. Does anyone know what I have done wrong in my code?

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135

2 Answers2

1

You need to build a new robber name and then return that:

def translate(robber):
    consonants = 'bcdfghjklmnpqrstvwxz'
    new_robber = ''
    for letter in robber:
        if letter in consonants:
            new_robber += letter + 'o' + letter
        else:
            new_robber += letter
    return new_robber

And the bonus answer which actually contains a nested loop (but is not very good python):

def translate(robber):
    consonants = 'bcdfghjklmnpqrstvwxz'
    new_robber = ''
    for letter in robber:
        for consonant in consonants:
            if consonant == letter:
                letter += 'o' + letter
                break
        new_robber += letter
    return new_robber
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
1

Use print ... for python 2.7

def translate(s):
  consonants = 'bcdfghjklmnpqrstvwxz'
  return ''.join(x + 'o' + x if x in consonants else x for x in s)

print(translate("robbers language"))
old_guy
  • 23
  • 6