Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Julius Caesar密码程序_Python_String_Python 3.x - Fatal编程技术网

Python Julius Caesar密码程序

Python Julius Caesar密码程序,python,string,python-3.x,Python,String,Python 3.x,我试图制作Julius Caesar密码程序,但在句子的开头和结尾添加了一个随机字母,这增加了一个转折点。由于某种原因,当我输入一个长字符串时,字符串的一部分在打印时丢失。我正在使用Python3。有人能解释一下如何解决这个问题以及为什么会发生这种情况吗?多谢各位 import random alpha = 'abcdefghijklmnopqrstuvwxyz' alphaupper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' def encode(cleartext):

我试图制作Julius Caesar密码程序,但在句子的开头和结尾添加了一个随机字母,这增加了一个转折点。由于某种原因,当我输入一个长字符串时,字符串的一部分在打印时丢失。我正在使用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()
输出:

Julias Cesar 13字母移位
你想解码还是编码:编码
你想编码什么:这个程序出于某种原因删除了这个字符串的一部分
编码:

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: 

我想我知道这里的问题

这两条线:

words = cleartext.replace(cleartext[len(cleartext) - 1], "")
words = words.replace(words[0], "")
这里的问题是您选择按值删除,而不是按索引删除

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

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

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

此外,您使用的函数是错误的。它应该给你一根新的绳子, 当它的参数是子字符串和要替换的子字符串时。 替换搜索子字符串的所有实例并替换它们。 它不应该像那样移除它

因此,它开始删除较大消息中的部分消息的原因 可能是因为您总是删除随机字符的所有实例, 字符串越长,就越有可能包含随机字符

无论如何,我喜欢使用:

words = cleartext[1:len(cleartext)]
当我做那样的事的时候

我也不认为这样做是个好主意:

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
参数,则替换所有出现的。这意味着您删除的角色比预期的要多

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

words = cleartext[1:-1]

这要干净得多,因为你实际上不在乎第一个和最后一个字符是什么,你只想让它们消失。

注意:rot13的要点是,你不需要单独的编码和解码功能,因为它们是一样的。即使你做了添加/删除填充字符之类的事情,你也应该将rot13部分分解成一个单独的方法来删除代码重复,减少错误的发生,并使代码更易于维护听起来是个不错的主意,除了在编码时,我想在开头和结尾添加额外的字母,使其更难解码。当解码时,如果它应该是一个函数,它将添加额外的字母并解码过去的字符串,而不是删除额外的字母并解码到原始字符串中。“如果它应该是一个函数”-它不应该在一个函数中。从来没有一个要求(可能是课外作业和谜题)在一个函数中完成所有事情。如果你能把一系列的问题分解成更小的部分,这样你就可以构建出更大的程序,并且仍然可以维护。好的,非常感谢你的澄清,在阅读了上面的答案后,我觉得自己很傻,因为这可能是我应该知道的。我想这是学习过程的一部分,作为初学者,这只会让我们想学更多。谢谢你澄清并花时间回答我的问题。好的,非常感谢你澄清,在阅读了张贴的答案后,我觉得自己很傻,因为这可能是我应该知道的。我想这是学习过程的一部分,作为初学者,这只会让我们想学更多。谢谢你澄清并花时间回答我的问题。