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

How to print only the result of the encryption and not the intermediate steps?

我在向用户显示我的八字符密钥时遇到问题,我希望程序只显示最后一行,但我不知道该怎么做。

输出如下所示:

这个程序有三个选择。

  1. Encrypt a message.

  2. Decrypt the message.

  3. Exit the program.

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']

这个程序我想要的只是打印八个字符的最后一行key.I 如果你们改变输出,我不介意。请记住,您需要查看的代码位于名为 '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()

我想将 print 语句移出 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))