【发布时间】:2022-12-10 16:31:26
【问题描述】:
from string import ascii_lowercase as alphabet1
from string import ascii_uppercase as alphabet2
import letter as letter
def cipher(user_input, shift):
cipher1 = {char: alphabet1[(i + shift) % 26] for i, char in enumerate(alphabet1)}
cipher2 = {char: alphabet2[(i + shift) % 26] for i, char in enumerate(alphabet2)}
caesar_cipher = ""
for letter in user_input:
caesar_cipher += cipher1.get(letter, letter)
else:
caesar_cipher += cipher2.get(letter, letter)
return caesar_cipher
if __name__ == "__main__":
user_input = input("Enter the String: ")
shift = int(input("Enter shift: "))
print("Caesar Cipher: " + cipher(user_input, shift))
我正在为大写和小写字符执行 Caeser 密码。 但结果不正确。 cipher1 用于小写,密码 2 用于大写。我已经在函数中定义了它。并在 main 方法中调用它 小写得到的结果是:
Enter the String: abc
Enter shift: 2
Caesar Cipher: cdec
应该是cde
大写得到的结果是:
Enter the String: ABC
Enter shift: 2
Caesar Cipher: ABCE
应该是CDE
【问题讨论】:
-
...the result is not correct.。怎么不对呢?你用什么作为输入,输出是什么?你期望它是什么?您是否怀疑代码的特定部分? -
How to step through Python code to help debug issues?如果你用的是IDE现在是学习它的调试特性的好时机 或者内置的Python debugger。印刷东西在你的程序中的战略点可以帮助你跟踪正在发生或没有发生的事情。 What is a debugger and how can it help me diagnose problems?。
-
collections.deque 有一个可能有用的旋转方法。
-
我已经更新了结果,请检查
标签: python caesar-cipher