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)
尝试创建一个带有字符串并将其转换为强盗语言的嵌套循环。有谁知道我的代码中我做错了什么?
答案 0 :(得分:1)
您需要构建一个新的强盗名称,然后返回:
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
奖金答案实际上包含一个嵌套循环(但不是很好的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
答案 1 :(得分:1)
使用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"))