-1

我有一个代码,但无法使其具有交互性。这是问题所在:

"""编写一个名为 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()

我不知道我的输入应该在函数中的哪个位置进行。我有一种感觉它可以被加密?

4

2 回答 2

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和此处的 Python 文档。另外,请注意我们的 rot 函数仅适用于小写字母。如果您尝试使用大写字符或任何不在.alphabet

于 2017-08-27T03:24:18.233 回答
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))
于 2017-08-27T02:36:39.760 回答