2014-09-20 98 views
-3

我試圖讓程序編碼字符串不同的字符串,使用Caesar Code我的程序不工作,我不知道爲什麼

def encode(letters,code,word): 
    lettersTab = list(letters) 
    lettersTab = lettersTab*2 
    lenghtLetters = len(letters) 
    howMany = code%lenghtLetters 
    wordTab = list(word) 
    secondTab = [None]*len(word) 
    for j in range(0,len(word)): 
     for i in range(0,int(len(lettersTab)/2)): 
      secondTab[j] = wordTab[j].replace(lettersTab[i],lettersTab[i+howMany]) 
    print(secondTab) 
encode("bca",2,"abc") 

這意味着輸入是b轉換爲等。B-> A,C> b時,A->ç

輸出應爲:

['c','a','b'] 

但它是

['b','b','c']

+0

我認爲你需要發佈更多的代碼 - 包含你的編譯/語法錯誤ISN行目前在你的片段。 – 2014-09-20 15:04:40

+1

您在以'secondTab [j] = ...'開頭的行上缺少和結尾括號。 – 2014-09-20 15:04:46

+1

您是否嘗試用鉛筆和紙張手工瀏覽代碼*?將此打印語句添加到內部循環的* top *中。它可能會幫助您診斷問題。 '''print'{} \ t {:<18} \ t {} \ t {}'。格式((j,i),secondTab,wordTab,lettersTab)''' – wwii 2014-09-20 15:53:04

回答

0

你的算法不會做你所期望的,因爲內循環

for i in range(0,int(len(lettersTab)/2)):

是在循環的每一步設置secondTab[j]wordTab[j],即使wordTab[j]不匹配lettersTab[i],這是不是你想要的。

請注意,newstr = oldstr.replace(a, b)使newstr = oldstr如果oldstr不包含a

所以這裏是你的函數的一個清理版本,加上幾個漸進的改進,最後的例子使用列表理解。我的一些版本使用index(),如果它找不到它正在查找的內容,將會拋出異常,但是您的原始代碼也會遇到類似的問題。

我改變了函數的名稱,因爲encode是Python 2(我正在運行)中的標準字符串函數。

#! /usr/bin/env python 

''' 
Caeser encryption 
From http://stackoverflow.com/questions/25950099/my-program-dont-work-i-dont-know-why 
''' 

import sys 

def caeser_encode0(letters, code, word): 
    lettersTab = list(letters) * 2 
    howMany = code % len(letters) 
    wordTab = list(word) 
    secondTab = [None] * len(word) 
    for j in range(0, len(word)): 
     for i in range(0, int(len(lettersTab)/2)): 
      if wordTab[j] == lettersTab[i]: 
       secondTab[j] = wordTab[j].replace(lettersTab[i], lettersTab[i + howMany]) 
       print(wordTab[j], lettersTab[i], lettersTab[i + howMany], secondTab) 
    print(secondTab) 


def caeser_encode1(letters, code, word): 
    lettersTab = list(letters) * 2 
    howMany = code % len(letters) 
    wordTab = list(word) 
    secondTab = [] 
    for ch in word: 
     for i in range(0, int(len(lettersTab)/2)): 
      if ch == lettersTab[i]: 
       secondTab.append(lettersTab[i + howMany]) 
       print(ch, lettersTab[i], lettersTab[i + howMany], secondTab) 
    print(secondTab) 


def caeser_encode2(letters, code, word): 
    lettersTab = list(letters) * 2 
    howMany = code % len(letters) 
    secondTab = [] 
    for ch in word: 
     i = lettersTab.index(ch) 
     secondTab.append(lettersTab[i + howMany]) 
     print(ch, lettersTab[i], lettersTab[i + howMany], secondTab) 
    print(secondTab) 


def caeser_encode3(letters, code, word): 
    howMany = code % len(letters) 
    secondTab = [] 
    for ch in word: 
     i = letters.index(ch) 
     newi = (i + howMany) % len(letters) 
     secondTab.append(letters[newi]) 
     print(ch, letters[i], letters[newi], secondTab) 
    print(secondTab) 


def caeser_encode4(letters, code, word): 
    howMany = code % len(letters) 
    secondTab = [] 
    for ch in word: 
     newi = (letters.index(ch) + howMany) % len(letters) 
     secondTab.append(letters[newi]) 
     print(ch, letters[newi], secondTab) 
    print(secondTab) 


def caeser_encode(letters, code, word): 
    howMany = code % len(letters) 
    secondTab = [ 
     letters[(letters.index(ch) + howMany) % len(letters)] for ch in word] 
    print(secondTab) 


def main(): 
    caeser_encode("bca", 2, "abc") 

if __name__ == '__main__': 
    main() 

希望你(或某人:))會更加了解Python從這些例子...

相關問題