簡體   English   中英

如何重新處理此ROT13功能

[英]How to re-work this ROT13 Function

我知道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

當您編寫自己時,您正在打印結果。 在Web應用程序中,按標准輸出打印不起作用,因為它們通常使用協議(unix套接字等)將數據傳遞回Web服務器進程(或者使用Twisted等基於Python的Web服務器,在這種情況下標准輸出將轉到您開始該過程的控制台)。

因此,再次編寫時,您需要修改函數以返回值而不是打印它。 這樣做有無數種方法,最簡單的方法就是用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)

您只是在打印出值,而不是積累爛攤子。 實際上,您要返回的函數本身是不對的。

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)

運行腳本將產生:

$ ./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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM