我有一个代码,但无法使其具有交互性。这是问题所在:
"""编写一个名为 rot13 的函数,它使用凯撒密码来加密消息。凯撒密码的工作方式类似于替换密码,但每个字符都被字母表中“其右边”的 13 个字符替换。例如,字母“a”变成字母“n”。如果一个字母超过了字母表的中间,那么计数会再次绕到字母“a”,所以“n”变成“a”,“o”变成“b”并且等等。提示:每当您谈论围绕它的事情时,考虑模算术(使用余数运算符)是一个好主意。“”“
这是这个问题的代码:
def rot13(mess):
alphabet = 'abcdefghijklmnopqrstuvwxyz'
encrypted = ''
for char in mess:
if char == ' ':
encrypted = encrypted + ' '
else:
rotated_index = alphabet.index(char) + 13
if rotated_index < 26:
encrypted = encrypted + alphabet[rotated_index]
else:
encrypted = encrypted + alphabet[rotated_index % 26]
return encrypted
def main():
print(rot13('abcde'))
print(rot13('nopqr'))
print(rot13(rot13('since rot thirteen is symmetric you should see this message')))
if __name__ == "__main__":
main()
我想让它具有交互性,您可以在其中输入任何消息,并且可以根据需要多次旋转字母。这是我的尝试。我知道您需要两个参数才能传递,但我不知道如何替换一些项目。
这是我的尝试:
def rot13(mess, char):
alphabet = 'abcdefghijklmnopqrstuvwxyz'
encrypted = ''
for char in mess:
if char == ' ':
encrypted = encrypted + ' '
else:
rotated_index = alphabet.index(char) + mess
if rotated_index < 26:
encrypted = encrypted + alphabet[rotated_index]
else:
encrypted = encrypted + alphabet[rotated_index % 26]
return encrypted
def main():
messy_shit = input("Rotate by: ")
the_message = input("Type a message")
print(rot13(the_message, messy_shit))
if __name__ == "__main__":
main()
我不知道我的输入应该在函数中的哪个位置进行。我有一种感觉它可以被加密?