Skip to main content
 首页 » 编程设计

python之将字符串编码为以破折号分隔的数字,将以破折号分隔的数字解码为字符串

2024年12月31日20think

Task: Write a program that gives the user a choice to encode or decode. You will either encode letters into numbers separated by dashes, or decode a series of numbers (also separated by dashes) into letters.

我正在学习 python,事实证明这个实验室非常困难,因为我还不知道让它发挥作用所需的所有工具。我设法让编码部分工作,但我的解码部分很糟糕。我认为它采用 2 位数字并将它们视为单独的数字(“19”作为“1”和“9”,因此返回“ai”而不是“s”)。我考虑过从使用索引切换到尝试使用 chr() 将数字转换为字母,但我也不熟悉它,在尝试添加 96 以获得正确的数字时,我总是遇到类型错误。 然后 encode_letters 函数有点笨拙,但它可以工作。但是,decode_numbers 函数让我感到不适。

谁能告诉我我做错了什么?

def encode_letters(): 
    global code_out 
    encryption_key = (('a','1'), ('b','2'), ('c','3'), ('d','4'), ('e','5'), ('f','6'), ('g', '7'), ('h','8'), ('i','9'), ('j','10'), ('k','11'), ('l','12'), 
        ('m','13'), ('n','14'), ('o','15'), ('p','16'), ('q','17'), ('r','18'), ('s','19'), ('t','20'), ('u','21'), ('v','22'), ('w','23'), ('x','24'), 
        ('y','25'), ('z','26')) 
    msg_in = str(input("Enter the message you wish to encode:\n")) 
    msg_in = msg_in.lower() 
    from_index = 0 
    to_index = 1 
    for i in msg_in: 
        letter_found = False 
        for e in encryption_key: 
            if ('a' <= i and i <= 'z') and i == e[from_index]: 
                code_out = code_out + e[to_index] + "-" 
                letter_found = True 
        if not letter_found: 
            code_out = code_out + i 
 
    return code_out 
 
def return_encoded(): 
    global code_out 
    code_out = code_out.rstrip("-") 
    print("Your secret code is:", code_out.replace('- ', ' ')) 
 
def decode_numbers(): 
    global string_out 
    encryption_key = (('a','1'), ('b','2'), ('c','3'), ('d','4'), ('e','5'), ('f','6'), ('g','7'), ('h','8'), ('i','9'), ('j','10'), ('k','11'), ('l','12'), 
        ('m','13'), ('n','14'), ('o','15'), ('p','16'), ('q','17'), ('r','18'), ('s','19'), ('t','20'), ('u','21'), ('v','22'), ('w','23'), ('x','24'), 
        ('y','25'), ('z','26')) 
    numbers_in = input("Enter the numbers separated by dashes that you wish to decode: ") 
    numbers_in = numbers_in.replace('-', ' ') 
    print(numbers_in) 
    from_index = 1 
    to_index = 0 
 
    for i in numbers_in: 
        number_found = False 
        for e in encryption_key: 
            if i == e[from_index]: 
                string_out = string_out + e[to_index] 
                number_found = True 
        if not number_found: 
            string_out = string_out + i 
    return string_out 
 
def return_decoded(): 
    global string_out 
    print("Your decoded string is: ", string_out.capitalize()) 

请您参考如下方法:

如果您跟踪类型转换,我认为您可以大大简化此过程:

def convert(s, method='encode'): 
  if method == 'encode': 
    return '-'.join([str(ord(i)) for i in s]) 
  elif method == 'decode': 
    return ''.join([str(chr(int(i))) for i in s.split('-')]) 
 
s = 'cats on wheels' 
encoded = convert(s, method='encode') 
decoded = convert(encoded, method='decode') 
 
print(encoded) # prints 99-97-116-115-32-111-110-32-119-104-101-101-108-115 
print(decoded) # prints cats on wheels 

正如您所说,可以使用 ord 将字符串转换为整数,然后使用 chr 将整数转换回字符串。这让我们可以将字符串翻转为连字符分隔的整数序列,然后将该序列翻转回输入字符串