Python使用备用字母表加密字符串

Python使用备用字母表加密字符串,python,string,encryption,Python,String,Encryption,我不知道该怎么做。我需要加密给定不同字母表的字符串 def substitute(string, ciphertext): alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" encrypted = [] list(alphabet) list(ciphertext) encrypted = "" for x in string: if x.isalpa(): encrypted.

我不知道该怎么做。我需要加密给定不同字母表的字符串

def substitute(string, ciphertext):
    alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    encrypted = []
    list(alphabet)
    list(ciphertext)
    encrypted = ""
    for x in string:
        if x.isalpa():
            encrypted.append(ciphertext[x])
        else:
            encrypted.append(x)
            word = string.join(encrypted)
    print(encrypted)

    return encrypted
试试这个:

def substitute(string, ciphertext):
    alphabet = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ") # list() returns a list,
    ciphertext = list(ciphertext) # it doesn't change (mutate) the variable
    encrypted = [] # Not sure why you were storing the empty string here,
                   # but strings cannot use the append() method.
    for x in string:
        if x.isalpha(): # Fixed a typo
            # Here I think you want to use alphabet.index(x) instead of x.
            encrypted.append(ciphertext[alphabet.index(x)])
        else:
            encrypted.append(x)
    return "".join(encrypted) # Turning the list into a string
正如另一位评论者所说,在将来,请添加示例,说明您做了什么,而不是您的代码要做什么


我建议你查一下易变性的定义,因为这似乎是你正在努力解决的问题。

到底什么不起作用?你能提供一个预期输入和输出的例子吗?不是加密,而是编码。你知道的越多。。。