基于密钥的解码器程序不工作

2024-04-26 06:33:28 发布

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

我在试着编一个程序来解码编码的信息。我已经为大写字母和小写字母创建了两个单独的列表。但每次我输入一条带有空格的消息时,消息的第一个字母都会在每个单词之后保持输出。你知道吗

    n1=1
    while n1!=0:
        n=input("\nEnter code")
        n1=int(n[0])
        newcode=n[1:]
        list1=['a','b','c','d','e','f','g','h','i','j','k','l'
           ,'m','n','o','p','q','r','s','t','u','v','w','x','y','z']
        list2=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R'
           ,'S','T','U','V','W','X','Y','Z']
        list3=[]
        list4=[]
        for x in newcode:
            if i in list1:
                n=list1.index(i)
                n2=n-n1
                y=list1[n2]
                print(y, end="")
            else:
                list3.append(i)
                for x in list3:
                    if i in list2:
                        n=list2.index(i)
                        n2=n-n1
                        y=list2[n2]
                        print(y, end="")
                    else:
                        print(i, end="")

                    if n1==0:
                        '/n'
                        break

Sample input: 3Wkh fdu lu eoxh

Sample ouput: TheT carT isT blue

Sample input: 9Rc'b j carlthxwn

Sample output: ItI'sI' aI' trickyI' one


Tags: samplein消息forinputindexifend
1条回答
网友
1楼 · 发布于 2024-04-26 06:33:28

我可以毫无问题地运行你的程序并复制。问题不仅在于每个单词后面的第一个字母都是重复的。事实上,对于小写字母来说,所有的方法都很好,但是大写字母空格和其他空格会不断累积,并且不断重复。你知道吗

所有这些都来自这一行:list3.append(x)list3形式累加的字母,而不是小写字母。你知道吗

我没有花足够的时间来全面分析您的脚本,但这里有一个快速而肮脏的解决方案:

  • 将第10行:list3 = []替换为list3 = [''](即使list3成为一个包含1个元素的列表)
  • 将第19行:list3.append(x)替换为list3[0] = x(即保持list3的大小为1,并将新元素放在前一个元素的列中)

我做到了,通过你的示例输入,我得到:

The car ir blue
It's a trickyone

(请注意,空格不再重复)

相关问题 更多 >