Python使用备用字母对字符串进行加密

2024-05-18 19:14:44 发布

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

我不知道该怎么做。我需要用不同的字母来加密一个字符串。在

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

Tags: 字符串inforstringifdef字母list
1条回答
网友
1楼 · 发布于 2024-05-18 19:14:44

试试这个:

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

正如另一位评论者所说,在将来,请添加您的代码要做什么和不做什么的示例。在

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

相关问题 更多 >

    热门问题