我需要对 Python 中输入的消息进行编码

I need to encode a message from an input in Python

所以我必须对消息进行编码,但它是一种不同的编码,如果输入是 CAT 输出必须是 DQ6 它应该编码将输入的每个字母更改为左上角键盘上的键,例如再次:in: bear out: G3Q4。我试着在字典中这样编码:

d1 = {"q": 1,"Q": 1,"w": 2,"W": 2,"e": 3,"E": 3,"r": 4,"R": 4,"t": 5,"T": 5,"y": 6,"Y": 6,"u": 7,"U": 7,"i": 8,"I": 8,"o": 9,"O": 9,"p": 0,"P": 0}

d2 = {"a": 'Q',"A": 'Q',"s": 'W',"S": 'W',"d": 'E',"D": 'E',"f": 'R',"F": 'R',"g": 'T',"G": 'T',"h": 'Y',"H": 'Y',"j": 'U',"J": 'U',"k": 'I',"K": 'I',"l": 'O',"L": 'O',"ñ": 'P',"Ñ": 'P'}

d3 = {"z": 'A',"Z": 'A',"x": 'S',"X": 'S',"c": 'D',"C": 'D',"v": 'F',"V": 'F',"b": 'G',"B": 'G',"n": 'H', "N": 'H',"m": 'J',"M": 'J',",": 'K',".": 'L',"-": 'Ñ'}

我尝试使用此功能来检查每个键,但我得到的所有值都是“None”。

text = input("Text: ")
def cif(text):
    cifrd = ""
    for i in text:
        if i in d1:
            cifrd += d1[(d1.index(i))%(len(d1))]
        elif i in d2:    
            cifrd += d2[(d2.index(i))%(len(d2))]
        elif i in d3:    
            cifrd += d3[(d3.index(i))%(len(d3))]
        else:
            cifrd += i
print("New text: ",cif(cifrd))

感谢任何帮助。

您的编码:

d1 = {"q": 1,"Q": 1,"w": 2,"W": 2,"e": 3,"E": 3,"r": 4,"R": 4,"t": 5,"T": 5,"y": 6,"Y": 6,"u": 7,"U": 7,"i": 8,"I": 8,"o": 9,"O": 9,"p": 0,"P": 0}

d2 = {"a": 'Q',"A": 'Q',"s": 'W',"S": 'W',"d": 'E',"D": 'E',"f": 'R',"F": 'R',"g": 'T',"G": 'T',"h": 'Y',"H": 'Y',"j": 'U',"J": 'U',"k": 'I',"K": 'I',"l": 'O',"L": 'O',"ñ": 'P',"Ñ": 'P'}

d3 = {"z": 'A',"Z": 'A',"x": 'S',"X": 'S',"c": 'D',"C": 'D',"v": 'F',"V": 'F',"b": 'G',"B": 'G',"n": 'H', "N": 'H',"m": 'J',"M": 'J',",": 'K',".": 'L',"-": 'Ñ'}

有几个问题。看我的评论

text = input("Text: ")
def cif(text):
    cifrd = ""
    for letter in text:
        # There is no need to manually write out each dictionary and do a check
        # Put the dictionaries in a list, iterate over each one, and if the letter
        # is in the dictionary, you will get the respective letter back
        for encode in [d1, d2, d3]:
            # check if my letter is in the dictionary
            actual = encode.get(letter)
            # If you check a dictionary and the key is not there, you will get `None`, this if statement ensures you only append actual number/characters
            if actual:
                cifrd += str(actual)
    # When using a function, return something if you need it outside of the function
    return cifrd

decoded = cif(text)
print("New text: {}".format(decoded))

您的代码存在一些问题:

  1. 您需要 return cif() 函数末尾的“编码”文本
  2. 您需要将 text 变量传递给 cif() 函数,而不是 cifrd ,后者未在函数外部定义
  3. 词典没有 .index() 方法,您可以通过按键访问词典项目,例如 d1["q"] returns 1.

就其价值而言,没有必要维护三个单独的词典,也没有理由在您的词典中同时维护小写字母和 upper-cased 字母;存储 lower-cased upper-cased 键,并在访问翻译时将输入转换为正确的大小写,即 input "Q" -> lowercase "q" -> d1["q"].

这里:

mapping = {'q': 1, 'w': 2, 'e': 3, 'r': 4, 't': 5, 'y': 6, 'u': 7, 'i': 8, 'o': 9, 'p': 0,
           'a': 'q', 's': 'w', 'd': 'e', 'f': 'r', 'g': 't', 'h': 'y', 'j': 'u', 'k': 'i', 'l': 'o', 'ñ': 'p',
           'z': 'a', 'x': 's', 'c': 'd', 'v': 'f', 'b': 'g', 'n': 'h', 'm': 'j', ',': 'k', '.': 'l', '-': 'ñ'}


def cif(s: string) -> string:
    encoded_string = ""
    for char in s:
        encoded_string += mapping.get(char.lower(), char)  # leaves the character un-encoded, if the character does not have a mapping
    return encoded string

我实际上建议使用 str.translate()。您可以传递两个字符串,第一个是输入字符,第二个是这些输入应映射到的字符:

t = str.maketrans("qwertyuiopasdfghjklñzxcvbnm,.-", "1234567890qwertyuiopasdfghjklñ")


"hello world".translate(t)
'y3oo9 294oe'