Python-Julius-Caesar密码程序

2024-04-20 11:36:22 发布

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

我试图使朱利叶斯凯撒密码程序,但增加了一个扭曲,增加了一个随机字母在句首和句尾。由于某些原因,当我输入一个长字符串时,部分字符串在打印时丢失。我使用的是python3。有人能解释一下如何解决这个问题吗?为什么会发生这种情况?谢谢你

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()

输出:

^{pr2}$

编码:

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: 

Tags: toinalphayoulikeencodewordsprint
2条回答

我想我知道这里的问题。在

这两条线:

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

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

这适用于对象数组(例如,如果使用remove), 因为对象的每个实例总是有不同的对象引用。在

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

不管怎样,当你想用索引替换时,用索引替换是一个好习惯。在

而且你用错了函数。它应该给你一条新的线, 当它的参数是子字符串和要替换的子字符串时。 替换搜索子字符串的所有实例并替换它们。 它不应该那样把它取下来。在

所以,它开始删除大消息中部分消息的原因 可能是因为你总是删除一个随机字符的所有实例, 字符串越长,其中就越有可能包含随机字符。在

总之,我喜欢使用:

^{pr2}$

当我做那样的事时。在

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

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

主要是因为,尽管你可能不知道,但是每一次

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

这不仅适用于递归调用,而且适用于大多数函数调用。在

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

尝试使用

if __name__ == "__main__":
    while True: men()

问题是,解码的时候,你会这么做

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

如果不包含可选的第三个count参数,^{}将替换所有个实例。这意味着你删除的字符比你预想的要多。在

如果您只想从字符串中去掉第一个和最后一个字符,可以执行以下操作

^{pr2}$

这更简洁了,因为你不在乎第一个和最后一个字符是什么,你只想让它们消失。在

相关问题 更多 >