我的程序不起作用,我不知道为什么

2024-07-04 16:40:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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转换成a,等等。b->;a,c->;b,a->;c

输出应为:

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

但确实如此

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


Tags: 字符串ingtforlenrangelistword
1条回答
网友
1楼 · 发布于 2024-07-04 16:40:38

因为内部循环

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

在循环的每个步骤上将secondTab[j]设置为wordTab[j],即使wordTab[j]lettersTab[i]不匹配,这不是您想要的。你知道吗

注意,如果oldstr不包含a,则newstr = oldstr.replace(a, b)使newstr=oldstr。你知道吗

下面是一个清理过的函数版本,加上一些渐进式的改进,最后一个示例使用了列表理解。我的一些版本使用index(),如果找不到它要找的东西,它会抛出一个异常,但是您的原始代码也遇到了类似的问题。你知道吗

我更改了函数的名称,因为encode是python2(我正在运行)中的标准字符串函数。你知道吗

#! /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的信息。。。你知道吗

相关问题 更多 >

    热门问题