Currently I have a list of substitutions for specific letters in the alphabet. The letter a
is replaced by whatever value is at the first index, b
with whatever is at the second index, and so on.
One feature that I appreciate in Python is the ability to perform list comprehensions. However, when I attempted to perform this with concatenation, I did not get positive results.
letters = ["ka","zu","mi","te","ku","lu","ji","ri","ki","zu","me","ta","rin","to","mo",
"no","ke","shi","ari","chi","do","ru","mei","na","fu","zi"]
def nameToNinja(str):
name = ""
for i in str:
i=i.lower()
if ord(i) >= 97 and ord(i) <= 122:
name+= letters[ord(i.lower()) - 97]
else:
name += i
return name
name = "Obama"
print("Your ninja name is: {0}".format(nameToNinja(name)))
My attempt at turning the function into a list comprehension in Python does not work. In fact, the only error I am receiving is Syntax Error
.
Attempt:
def nameToNinja(str):
return ''.join([letters[ord(i.lower()) - 97] if ord(i.lower()) >= 97 and ord(i.lower()) <= 122 else i
for i in str)
What is the correct way to shorten the original function into a concatenated list comprehension.