如何仅打印加密结果而不打印中间步骤?

时间:2015-01-31 16:10:35

标签: python encryption

我向用户显示我的八个字符键有问题,我希望程序只显示最后一行,但我不知道如何。

输出如下:

这个程序有三个选择。

  
      
  1. 加密邮件。

  2.   
  3. 解密消息。

  4.   
  5. 退出该计划。

  6.   
Make your choice: 1

以下文字将加密:

在拉曼查的某个地方,在一个我不记得的名字的地方,一位绅士不久前生活过,其中一位在架子上有长矛和古盾的人,留着一个瘦小的唠叨和一个赛狗的灰狗。

This is your eight character key.     
 ['H']
The last row is your eight character key. 
 ['H', 'D']
This is your eight character key.
 ['H', 'D', 'z']
This is your eight character key.
 ['H', 'D', 'z', '#']
The last row is your eight character key.
 ['H', 'D', 'z', '#', "'"]
This is your eight character key.
 ['H', 'D', 'z', '#', "'", 'y']
This is your eight character key.
 ['H', 'D', 'z', '#', "'", 'y', 'i']
This is your eight character key.
 ['H', 'D', 'z', '#', "'", 'y', 'i', '1']

我想要的这个程序就是打印八个字符键的最后一行。如果你们改变输出,我不会介意。请记住,您需要查看的代码位于名为' EncryptCode'的过程中。 我的代码如下所示:

import random 
with open ("E:\Controlled Assessment CS\Controlled Assessment Sample.txt",mode="r",encoding="utf=8") as encrypt_file:
encrypt = encrypt_file.read()


def EncryptCode():
    print ("This text below will be encrypted:")
    printMessage(encrypt)
    eightNumKey = 0
    ASCIINumber = []
    ASCIIKey = []
    while eightNumKey !=8:
        eightNumKey = eightNumKey + 1
        RandomNumber = random.randint(33,126)
        ASCIINumber.append(RandomNumber)
        RandomCharacter = chr(RandomNumber)
        ASCIIKey.append(RandomCharacter)
        print ("This is your eight character key. \n",ASCIIKey)
def showMenu():
    choice = input("\n\nMake your choice: ")

    if choice == "1":
        EncryptCode()
        showMenu()

    elif choice == "2":
        DecryptCode()
        showMenu()

    elif choice not in ["1","2","3"]:
        print(choice,"Is not one of the choices")
        showMenu()

print("This program has three choices.")
print("\n1. Encrypt a message.")
print("\n2. Decrypt the message.")
print("\n3. Exit the program.")

showMenu()

1 个答案:

答案 0 :(得分:1)

我想将while语句移出while循环将完成你的工作,因为列表最后只打印一次。

def EncryptCode():
    print ("This text below will be encrypted:")
    printMessage(encrypt)
    eightNumKey = 0
    ASCIINumber = []
    ASCIIKey = []
    while eightNumKey !=8:
        eightNumKey = eightNumKey + 1
        RandomNumber = random.randint(33,126)
        ASCIINumber.append(RandomNumber)
        RandomCharacter = chr(RandomNumber)
        ASCIIKey.append(RandomCharacter)
    print ("This is your eight character key. \n",ASCIIKey)

但是,如果要打印字符串而不是整个列表,则可以使用.join()方法

print ("This is your eight character key. \n"," ".join(ASCIIKey))