2013-04-09 43 views
1

我知道ROT13有無數的方法,Python甚至有一個內置函數,但我真的很想了解如何修改我編寫的代碼。當我在我的編輯器中測試它時,它工作的很好(保留空格,標點符號和大小寫),但在我的網頁中不起作用。我被告知我只是將字符打印出來而不是將它們複製到結果字符串中。我已經玩了好幾個小時,但還沒有想出如何操作它來包含返回語句。如何重新使用這個ROT13函數

對不起,如果這是一個愚蠢的問題 - 我是一個新手:)任何幫助超級讚賞。

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 
+1

'def rot(xy):return xy.encode('rot13')'。 – 2013-04-09 17:24:02

回答

0
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 
0

你只是打印出來的值,你沒有建立腐爛。事實上,你正在返回函數本身,這是不正確的。

+0

謝謝。那麼是不是可以操縱這個代碼來建立腐爛?我應該重新開始嗎? – 2013-04-09 17:29:54

2

正如你自己寫的,你正在打印出結果。打印到標準輸出在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) 
+0

哦哇,真棒。使用列表很有意義!非常感謝!! – 2013-04-09 17:35:34

0
#!/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. 
0
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