我有一个代码而我无法将其设为交互式。 这是问题所在:
“”“编写一个名为rot13的函数,它使用Caesar密码来加密消息。凯撒密码的工作方式类似于替换密码,但每个字符在字母表中由字符13个字符替换为”其右边“。所以对于例如,字母“a”变为字母“n”。如果字母超过字母表的中间,则计数再次包围字母“a”,因此“n”变为“a”,“o”变为“ b“依此类推。提示:每当你谈到围绕它的好主意思考模运算时(使用余数运算符)。”“
以下是此问题的代码:
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()
我希望将其设置为交互式,您可以输入任何消息,并且可以根据需要多次旋转字母。这是我的尝试。我知道你需要传递两个参数,但我对如何替换一些项目毫无头绪。
这是我的尝试:
while()
我不知道我的输入应该在函数中发生什么。我有一种感觉可以加密吗?
答案 0 :(得分:0)
这可能就是你要找的东西。它通过messy_shit
输入旋转消息。
def rot13(mess, rotate_by):
alphabet = 'abcdefghijklmnopqrstuvwxyz'
encrypted = ''
for char in mess:
if char == ' ':
encrypted = encrypted + ' '
else:
rotated_index = alphabet.index(char) + int(rotate_by)
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))
答案 1 :(得分:0)
def rot(message, rotate_by):
'''
Creates a string as the result of rotating the given string
by the given number of characters to rotate by
Args:
message: a string to be rotated by rotate_by characters
rotate_by: a non-negative integer that represents the number
of characters to rotate message by
Returns:
A string representing the given string (message) rotated by
(rotate_by) characters. For example:
rot('hello', 13) returns 'uryyb'
'''
assert isinstance(rotate_by, int) == True
assert (rotate_by >= 0) == True
alphabet = 'abcdefghijklmnopqrstuvwxyz'
rotated_message = []
for char in message:
if char == ' ':
rotated_message.append(char)
else:
rotated_index = alphabet.index(char) + rotate_by
if rotated_index < 26:
rotated_message.append(alphabet[rotated_index])
else:
rotated_message.append(alphabet[rotated_index % 26])
return ''.join(rotated_message)
if __name__ == '__main__':
while True:
# Get user input necessary for executing the rot function
message = input("Enter message here: ")
rotate_by = input("Enter your rotation number: ")
# Ensure the user's input is valid
if not rotate_by.isdigit():
print("Invalid! Expected a non-negative integer to rotate by")
continue
rotated_message = rot(message, int(rotate_by))
print("rot-ified message:", rotated_message)
# Allow the user to decide if they want to continue
run_again = input("Continue? [y/n]: ").lower()
while run_again != 'y' and run_again != 'n':
print("Invalid! Expected 'y' or 'n'")
run_again = input("Continue? [y/n]: ")
if run_again == 'n':
break
注意:创建一个字符列表然后加入它们以生成字符串而不是使用string = string + char
会更有效。请参阅方法6 here和Python文档here。另外,请注意我们的rot函数仅适用于字母表的小写字母。如果您尝试使用大写字符或alphabet
中不包含的任何字符来破坏邮件,则会中断。