【问题标题】:How to print only the result of the encryption and not the intermediate steps?如何仅打印加密结果而不打印中间步骤?
【发布时间】:2015-01-31 16:10:34
【问题描述】:

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

输出如下:

这个程序有三个选择。

  1. 加密消息。

  2. 解密消息。

  3. 退出程序。

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()

【问题讨论】:

标签: python encryption


【解决方案1】:

我猜想将 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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2019-03-11
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多