如何使用解码列表为codeMessage编码?

时间:2019-04-01 18:58:53

标签: python

我已经全部解码和编码了。但是我需要使用已经拥有的编码对其进行解码。我必须为此使用codeMessage列表。我需要使用for循环,并从列表中获取每个数字,对其进行解码,然后将其添加到字符串中。全部使用ASCII。

我尝试使用“ for in in message:”并将ord()更改为chr(),但是没有用。

这是我的工作代码:

def main():
    message = "We’re all fighting battles no one knows about."

    codeMessage = []

    message1 = [87, 101, 39, 114, 101, 32, 97, 108, 108, 32, 102, 105, 103, 104, 116, 105, 110, 103, 32, 98, 97, 116, 116, 108, 101, 115, 32, 110, 111, 32, 111, 110, 101, 32, 107, 110, 111, 119, 115, 32, 97, 98, 111, 117, 116, 46,]

    for m in message:
        codeMessage.append(ord(m))

    for c in codeMessage:
        print(c),

    print("This is what the numbers say translated back to english from ASCII:")
    print(chr(87) + chr(101) + chr(39) + chr(114) + chr(101) + chr(32) + chr(97) + chr(108) + chr(108) + chr(32) + chr(102) + chr(105) + chr(103) + chr(104) + chr(116) + chr(105) + chr(110) + chr(103) + chr(32) + chr(98) + chr(97) + chr(116) + chr(116) + chr(108) + chr(101) + chr(115) + chr(32) + chr(110) + chr(111) + chr(32) + chr(111) + chr(110) + chr(101) + chr(32) + chr(107) + chr(110) + chr(111) + chr(119) + chr(115) + chr(32) + chr(97) + chr(98) + chr(111) + chr(117) + chr(116) + chr(46))

我得到了预期的输出,但这不是我应该做的正确方法。

1 个答案:

答案 0 :(得分:0)

这是我实现的解决方案。它在我的IDE上打印正确的结果。希望它对您的学习体验有所帮助:

message = "We’re all fighting battles no one knows about."
codeMessage = []
message1 = [87, 101, 39, 114, 101, 32, 97, 108, 108, 32, 102, 105, 103, 104, 116, 105, 110, 103, 32, 98, 97, 116, 116, 108, 101, 115, 32, 110, 111, 32, 111, 110, 101, 32, 107, 110, 111, 119, 115, 32, 97, 98, 111, 117, 116, 46,]
for m in message:
    codeMessage.append(ord(m))

result = ""
for m in codeMessage:
    result = result + chr(m)

print(result)