Search code examples
pythonpython-2.7rot13

How to re-work this ROT13 Function


I know that there are countless ways to ROT13 and that Python even has a in-built function, but I really want to understand how to modify the code that I've written. It works fine when I test it in my editor (maintains white space, punctuation and case) but won't work in my web page. I've been told that I'm just printing the characters out and not copying them into the result string. I've played with it for hours but haven't yet figured out how to manipulate it to incorporate a return statement.

Sorry if this is a silly question - I am a newbie :) Any help is super appreciated.

dictionary = {'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(xy):

    for c in xy:

        if c.islower():
            print dictionary.get(c),

        if c.isupper():
            c = c.lower()
            result = dictionary.get(c)
            print result.capitalize(),

        if c not in dictionary:
            print c,

    return rot

Solution

  • dictionary = {'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(xy):
        rot13 = ''
        for c in xy:
            if c.islower():
                rot13 += dictionary.get(c)
            if c.isupper():
                c = c.lower()
                rot13 += dictionary.get(c).capitalize()
            if c not in dictionary:
                rot13 += c
        print "ROTTED: ", rot13  
        return rot13