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

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

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

问题

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

这是输出:

Q

M

D

X

我希望输出像这样:QMDX。

  1. 这是代码
  2. print("""
  3. 88 88
  4. "" 88
  5. 88
  6. ,adPPYba, 88 8b,dPPYba, 88,dPPYba, ,adPPYba, 8b,dPPYba,
  7. a8" "" 88 88P' "8a 88P' "8a a8P_____88 88P' "Y8
  8. 8b 88 88 d8 88 88 8PP""""""" 88
  9. "8a, ,aa 88 88b, ,a8" 88 88 "8b, ,aa 88
  10. `"Ybbd8"' 88 88`YbbdP"' 88 88 `"Ybbd8"' 88
  11. 88
  12. 88 """)
  13. 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']
  14. direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
  15. text = input("Type your message:\n").lower()
  16. shift = int(input("Type the shift number:\n"))
  17. def encrypt(text, shift):
  18. for i in text:
  19. encryption = ""
  20. i_unicode = ord(i)
  21. i_index = ord(i) - ord('A')
  22. new_index = (i_index + shift) % 26
  23. new_unicode = new_index + ord('A')
  24. new_character = chr(new_unicode)
  25. encryption = encryption + new_character
  26. print(encryption)
  27. 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.

  1. Here is the code:
  2. print("""
  3. 88 88
  4. "" 88
  5. 88
  6. ,adPPYba, 88 8b,dPPYba, 88,dPPYba, ,adPPYba, 8b,dPPYba,
  7. a8" "" 88 88P' "8a 88P' "8a a8P_____88 88P' "Y8
  8. 8b 88 88 d8 88 88 8PP""""""" 88
  9. "8a, ,aa 88 88b, ,a8" 88 88 "8b, ,aa 88
  10. `"Ybbd8"' 88 88`YbbdP"' 88 88 `"Ybbd8"' 88
  11. 88
  12. 88 """)
  13. 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']
  14. direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
  15. text = input("Type your message:\n").lower()
  16. shift = int(input("Type the shift number:\n"))
  17. def encrypt(text, shift):
  18. for i in text:
  19. encryption = ""
  20. i_unicode = ord(i)
  21. i_index = ord(i) - ord('A')
  22. new_index = (i_index + shift) % 26
  23. new_unicode = new_index + ord('A')
  24. new_character = chr(new_unicode)
  25. encryption = encryption + new_character
  26. print(encryption)
  27. encrypt(text, shift)

Thank you!

答案1

得分: 1

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

这将导致以下结果:

  1. Q
  2. QM
  3. QMD
  4. QMDX

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

然后它将输出:

  1. QMDX

如果有关如何在Python中创建凯撒密码的问题,一个更有效的方法是,如ShadowRanger建议的,使用string.maketrans

  1. import string
  2. def caeserEncode(target, shift):
  3. alphabet = \
  4. string.ascii_lowercase + \
  5. string.ascii_uppercase
  6. encodedAlphabet = \
  7. string.ascii_lowercase[shift:] + \
  8. string.ascii_lowercase[0:shift] + \
  9. string.ascii_uppercase[shift:] + \
  10. string.ascii_uppercase[0:shift]
  11. transTable = target.maketrans(alphabet, encodedAlphabet)
  12. return target.translate(transTable)
  13. print(caeserEncode("testString", 2))
  14. # 输出 vguvUvtkpi
  15. print(caeserEncode("abc xyz", 2))
  16. # 输出 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.

  1. def encrypt(text, shift):
  2. encryption = ""
  3. for i in text:
  4. i_unicode = ord(i)
  5. i_index = ord(i) - ord('A')
  6. new_index = (i_index + shift) % 26
  7. new_unicode = new_index + ord('A')
  8. new_character = chr(new_unicode)
  9. encryption = encryption + new_character
  10. print(encryption)

This will result in:

  1. Q
  2. QM
  3. QMD
  4. QMDX

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

  1. def encrypt(text, shift):
  2. encryption = ""
  3. for i in text:
  4. i_unicode = ord(i)
  5. i_index = ord(i) - ord('A')
  6. new_index = (i_index + shift) % 26
  7. new_unicode = new_index + ord('A')
  8. new_character = chr(new_unicode)
  9. encryption = encryption + new_character
  10. print(encryption)

Then it will output:

  1. 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:

  1. import string
  2. def caeserEncode(target, shift):
  3. alphabet = \
  4. string.ascii_lowercase + \
  5. string.ascii_uppercase
  6. encodedAlphabet = \
  7. string.ascii_lowercase[shift:] + \
  8. string.ascii_lowercase[0:shift] + \
  9. string.ascii_uppercase[shift:] + \
  10. string.ascii_uppercase[0:shift]
  11. transTable = target.maketrans(alphabet, encodedAlphabet)
  12. return target.translate(transTable)
  13. print(caeserEncode("testString", 2))
  14. #output vguvUvtkpi
  15. print(caeserEncode("abc xyz", 2))
  16. #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:

确定