如何修改我的凯撒密码实现以将结果输出为单个字符串?

huangapple go评论72阅读模式
英文:

How can I modify my Caesar cipher implementation to output the result as a single string?

问题

我试图编写一个凯撒密码;每当它打印结果时,每个字符串都会创建一个新行,而不是一个单独的字符串。

这是输出:

Q

M

D

X

我希望输出像这样:QMDX。

这是代码
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)

谢谢!

英文:

I'm trying to write a Ceasar cipher; whenever it prints the result, each string creates a new line instead of a single string.

This is the output:

Q

M

D

X

I wanted the output to go like this: 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)

Thank you!

答案1

得分: 1

需要在for循环之外初始化encryption。现在它的值在每次编码新字母时都被重置,所以它不会变得更长。

这将导致以下结果:

Q
QM
QMD
QMDX

如果你只想要它打印一次,把print语句放在for循环之外。

然后它将输出:

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))
# 输出 vguvUvtkpi

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

请注意,代码中的"是HTML实体,表示双引号,实际代码中应该使用双引号或单引号。

英文:

You need to initialize encryption outside the for loop. Right now it's value is being reset every time you encode a new letter, so it isn't getting any longer.

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)

This will result in:

Q
QM
QMD
QMDX

If you only want it to print once, put the print statement outside the for loop.

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)

Then it will output:

QMDX

In case this comes up for how to make a caeser cypher in python, a more efficient method is, as suggested by 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

huangapple
  • 本文由 发表于 2023年5月25日 09:51:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定