简体   繁体   中英

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

As you wrote yourself, you are printing the result out. Printing to standard out does not work in web applications, since they normally use a protocol (unix socket or the like) for communicating data back to the web server process (or you use a Python-based web server such as Twisted, in which case standard output will go to the console where you started the process).

So, again as you write, you need to modify the function in order to return the value instead of printing it. There are countless ways of doing this, the simplest being to just replace the standard output with a StringIO object:

from StringIO import StringIO

def rot(xy):
    rot = StringIO()
    for c in xy:

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

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

        if c not in dictionary:
            print >>rot, c,

    return rot.getvalue()

A more basic way to do it is to store the output in a list:

def rot(xy):
    rot = []
    for c in xy:

        if c.islower():
            rot.append(dictionary.get(c))

        if c.isupper():
            c = c.lower()
            result = dictionary.get(c)
            rot.append(result.capitalize())

        if c not in dictionary:
            rot.append(c)

    return ''.join(rot)

You're just printing out the values, you're not building up rot. In fact, you're returning the function itself, which isn't right at all.

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
#!/usr/bin/env python
import string

# Create alpha using 'string.ascii_uppercase' and 'string.ascii_lowercase' methods.
# Create rotated 13 using list/array positioning
# Create translation table using string.maketrans()
# Use translation table using string.translate()

# Get the alpha
alphaUpper=string.ascii_uppercase
rotatedUpper=alphaUpper[-13:] + alphaUpper[:-13]

alphaLower=string.ascii_lowercase
rotatedLower=alphaLower[-13:] + alphaLower[:-13]

combinedOriginal=alphaLower + alphaUpper
combinedRotated=rotatedLower + rotatedUpper

print combinedOriginal
print combinedRotated

translation_table = string.maketrans( combinedOriginal, combinedRotated )

message="This is the original message."

print message.translate(translation_table)
print message.translate(translation_table).translate(translation_table)

Running the script will yield:

$ ./rot.py 
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM
Guvf vf gur bevtvany zrffntr.
This is the original message.
alphabets = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
c_alphabets = []
for i in alphabets:
   c_alphabets.append(i.capitalize())
def rot13(s):
    out_string=''
    for i in s:
        if i in alphabets:
            j = alphabets[(alphabets.index(i) + 13) % 26]
            out_string += j
        elif i in c_alphabets:
            j = c_alphabets[(c_alphabets.index(i) + 13) % 26]
            out_string += j
        else:
            out_string += i
    return out_string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM