I have searched for this question and found some ways to do this. Some solutions were in one line too. There was one solution in which the data is stored in list and then the method append is used to make a word.
The other methods are working .
Though I want to know what is wrong with my code.
EDIT 1 : The problem with my code is that is shows only the first alphabet of the input string in the output instead of the whole word.
here is my code :
alph = {"a":"n" , "b":"o" ,"c":"p" , "d":"q","e":"r" , "f":"s" , "g":"t" ,"h":"u" , "i":"v", "j":"w", "k":"x" , "l":"y" , "m":"z" ,"n":"a", "o": "b" , "p":"c" ,"q":"d" ,"r":"e" ,"s":"f", "t":"g" ,"u":"h", "v":"i", "w":"j", "x":"k", "y":"l", "z":"m" }
def rot(word):
rt = ''
for c in word:
if c.islower():
rt += alph.get(c)
return rt
if c.isupper():
c = c.lower()
rt += alph.get(c).capitalize()
return rt
if c not in alph:
rt += c
return rt
word = raw_input("Enter:")
print rot(word)
Please check and tell me what is wrong in it.
string.maketrans(Py2)/str.maketrans(Py3) and then using the result to callstr.translate. No need for explicit loops at all.