Python 如何重新使用此ROT13函数

Python 如何重新使用此ROT13函数,python,python-2.7,rot13,Python,Python 2.7,Rot13,我知道ROT13有无数种方法,Python甚至有一个内置函数,但我真的想了解如何修改我编写的代码。当我在编辑器中测试它时(保持空白、标点符号和大小写),它工作正常,但在我的网页中不工作。有人告诉我,我只是将字符打印出来,而不是将它们复制到结果字符串中。我已经使用它好几个小时了,但还没有弄明白如何操纵它来合并一个return语句 对不起,如果这是一个愚蠢的问题-我是一个新手:)任何帮助都是超级感谢 dictionary = {'a':'n', 'b':'o', 'c':'p',

我知道ROT13有无数种方法,Python甚至有一个内置函数,但我真的想了解如何修改我编写的代码。当我在编辑器中测试它时(保持空白、标点符号和大小写),它工作正常,但在我的网页中不工作。有人告诉我,我只是将字符打印出来,而不是将它们复制到结果字符串中。我已经使用它好几个小时了,但还没有弄明白如何操纵它来合并一个return语句

对不起,如果这是一个愚蠢的问题-我是一个新手:)任何帮助都是超级感谢

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

您只是打印出值,并没有建立rot。事实上,您返回的是函数本身,这一点都不正确。

正如您自己编写的那样,您正在打印结果。打印到standard out在web应用程序中不起作用,因为它们通常使用协议(unix套接字等)将数据传回web服务器进程(或者使用基于Python的web服务器,如Twisted,在这种情况下,标准输出将转到启动进程的控制台)

因此,同样在编写时,您需要修改函数以返回值,而不是打印它。有无数种方法可以做到这一点,最简单的方法就是用
StringIO
对象替换标准输出:

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()
更基本的方法是将输出存储在列表中:

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)
运行脚本将产生:

$ ./rot.py 
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM
Guvf vf gur bevtvany zrffntr.
This is the original message.

def rot(xy):返回xy.encode('rot13')
。谢谢。那么,是否不可能操纵此代码来构建rot?我应该重新开始吗?哇,太棒了。使用列表非常有意义!非常感谢你!!
#!/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)
$ ./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