Python Julius Caesar密码程序

时间:2017-08-26 04:47:32

标签: python string python-3.x

我正在尝试制作Julius Caesar Cipher程序,但通过在句子的开头和结尾添加随机字母来添加扭曲。出于某种原因,当我输入长字符串时,字符串的一部分在打印时丢失。我正在使用python 3.有人可以解释如何解决这个问题以及为什么会发生这种情况?谢谢

import random
alpha = 'abcdefghijklmnopqrstuvwxyz'
alphaupper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'


def encode(cleartext):
    global alpha
    global alphaupper
    words = cleartext
    cyphertext = ""
    for char in words:
        if char in alphaupper:
            newpos = (alphaupper.find(char) + 13) % 26
            cyphertext += alphaupper[newpos]
        elif char in alpha:
            newpos = (alpha.find(char) + 13) % 26
            cyphertext += alpha[newpos]
        else:
            cyphertext += char

    cyphertext = alpha[random.randrange(len(alpha) - 1)] + cyphertext + alpha[random.randrange(len(alpha) - 1)]
    return cyphertext


def decode(cleartext):
    global alpha
    global alphaupper
    words = cleartext.replace(cleartext[len(cleartext) - 1], "")
    words = words.replace(words[0], "")
    cyphertext = ""
    for char in words:
        if char in alphaupper:
            newpos = (alphaupper.find(char) + 13) % 26
            cyphertext += alphaupper[newpos]
        elif char in alpha:
            newpos = (alpha.find(char) + 13) % 26
            cyphertext += alpha[newpos]
        else:
            cyphertext += char
    return cyphertext


print("Julias Ceasar 13 letter shift")


def men():
    words = input("Would you like to decode or encode: ")
    if "decode" in words:
        words = input("What would you like to decode: ")
        print(decode(words))
        print('\n')
        men()
    elif "encode" in words:
        words = input("What would you like to encode: ")
        print(encode(words))
        print('\n')
        men()
    else:
        print("Could not understand please try again")
        print('\n')
        men()


if __name__ == "__main__":
    men()

输出:

Julias Ceasar 13 letter shift
Would you like to decode or encode: encode
What would you like to encode: This program deletes parts of this string for some reason

ENCODED:

yGuvf cebtenz qryrgrf cnegf bs guvf fgevat sbe fbzr ernfbas

解码:

Would you like to decode or encode: decode
What would you like to decode: yGuvf cebtenz qryrgrf cnegf bs guvf fgevat sbe fbzr ernfbas

最终解密的句子:

This program deletes parts o this string or some reason


Would you like to decode or encode: 

2 个答案:

答案 0 :(得分:2)

看起来问题是,在解码时,你做了

words = cleartext.replace(cleartext[len(cleartext) - 1], "")
words = words.replace(words[0], "")
如果您不包含可选的第三个count参数,则

str.replace会替换所有次出现。这意味着你要移除的元素比你讨价还价的要多。

如果您只想删除字符串中的第一个和最后一个字符,则可以执行类似

的操作
words = cleartext[1:-1]

更干净,因为你实际上并不关心第一个和最后一个字符,你只是想让它们消失。

答案 1 :(得分:0)

我想我知道这里的问题。

这两行:

words = cleartext.replace(cleartext[len(cleartext) - 1], "")
words = words.replace(words[0], "")

这里的问题是您选择按值删除,而不是按索引删除。

这适用于,可能是一个对象数组(例如,如果你使用删除), 因为对象的每个实例总是有不同的对象引用。

(除非您使用类似arr [1] = arr [3]的内容,这意味着您复制了引用)。

无论如何,当你想用索引替换时,用索引替换它是个好习惯。

除此之外你还错误地使用了这个功能。它应该给你一个新的字符串, 当它的参数是子字符串和要替换的子字符串时。 替换搜索子字符串的所有实例并替换它们。 它不应该像那样删除它。

所以,它开始在较大的邮件上删除部分邮件的原因 可能是因为你总是删除随机字符的所有实例, 并且字符串越长,随机字符就越有可能。

无论如何,我喜欢使用:

words = cleartext[1:len(cleartext)]

当我做那样的事情时。

我也不认为做这样的递归调用是个好主意:

def men():
    input("something")
    men()

主要是因为,即使你可能不知道它,但每个和evey时间

你做一个递归调用它会保存你调用的位置。

这不仅适用于递归调用,也适用于大多数函数调用。

所以你要创建一个等于新int的东西,但你永远不会删除它。

尝试使用

if __name__ == "__main__":
    while True: men()
相关问题