python-3.x 如何修改Caesar密码实现以将结果输出为单个字符串?

cngwdvgl  于 2023-05-30  发布在  Python
关注(0)|答案(1)|浏览(117)

我在写凯撒密码每当打印结果时,每个字符串都会创建一个新行,而不是单个字符串。
这是输出:
Q类
M级
D
X
我希望输出是这样的:QMDX。

Here is the code:
print("""                                                             
           88             88                                 
           ""             88                                 
                          88                                 
 ,adPPYba, 88 8b,dPPYba,  88,dPPYba,   ,adPPYba, 8b,dPPYba,  
a8"     "" 88 88P'    "8a 88P'    "8a a8P_____88 88P'   "Y8  
8b         88 88       d8 88       88 8PP""""""" 88          
"8a,   ,aa 88 88b,   ,a8" 88       88 "8b,   ,aa 88          
 `"Ybbd8"' 88 88`YbbdP"'  88       88  `"Ybbd8"' 88          
              88                                             
              88                                      """)


alphabet = ['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']

direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))

def encrypt(text, shift):
    for i in text:
        encryption = ""
        i_unicode = ord(i)
        i_index = ord(i) - ord('A')
        new_index = (i_index + shift) % 26
        new_unicode = new_index + ord('A')
        new_character = chr(new_unicode)
        encryption = encryption + new_character

        print(encryption)

            
        
        

encrypt(text, shift)

谢谢你!

omqzjyyz

omqzjyyz1#

您需要在for循环外初始化encryption。现在它的值是被重置每次你编码一个新的字母,所以它不会得到任何更长的时间。

def encrypt(text, shift):
    encryption = ""
    for i in text:
        i_unicode = ord(i)
        i_index = ord(i) - ord('A')
        new_index = (i_index + shift) % 26
        new_unicode = new_index + ord('A')
        new_character = chr(new_unicode)
        encryption = encryption + new_character
        
        print(encryption)

这将导致:

Q
QM
QMD
QMDX

如果只想打印一次,请将print语句放在for循环之外。

def encrypt(text, shift):
    encryption = ""
    for i in text:
        i_unicode = ord(i)
        i_index = ord(i) - ord('A')
        new_index = (i_index + shift) % 26
        new_unicode = new_index + ord('A')
        new_character = chr(new_unicode)
        encryption = encryption + new_character
        
    print(encryption)

然后它将输出:

QMDX

如果这涉及到如何在python中创建凯撒密码,一个更有效的方法是,正如ShadowRanger所建议的那样,string.maketrans

import string

def caeserEncode(target, shift):
    alphabet = \
        string.ascii_lowercase + \
        string.ascii_uppercase

    encodedAlphabet = \
        string.ascii_lowercase[shift:] + \
        string.ascii_lowercase[0:shift] + \
        string.ascii_uppercase[shift:] + \
        string.ascii_uppercase[0:shift]
    transTable = target.maketrans(alphabet, encodedAlphabet)
    return target.translate(transTable)

print(caeserEncode("testString", 2))
#output vguvUvtkpi

print(caeserEncode("abc xyz", 2))
#cde zab

相关问题