Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 交互式rot13密码_Python_Python 3.x_Rot13 - Fatal编程技术网

Python 交互式rot13密码

Python 交互式rot13密码,python,python-3.x,rot13,Python,Python 3.x,Rot13,我有一个代码,我很难使它具有交互性。 问题是: “”“编写一个名为rot13的函数,该函数使用Caesar密码对消息进行加密。Caesar密码的工作原理类似于替换密码,但每个字符都被字母表中“其右侧”的13个字符替换。因此,例如,字母“a”变为字母“n”。如果一个字母超过了字母表的中间位置,那么计数又回到了字母“a”,因此“n”变成了“a”,“o”变成了“b”,依此类推。提示:每当你谈论环绕的事情时,最好考虑模运算(使用余数运算符)。” 以下是解决此问题的代码: def rot13(mess):

我有一个代码,我很难使它具有交互性。 问题是:

“”“编写一个名为rot13的函数,该函数使用Caesar密码对消息进行加密。Caesar密码的工作原理类似于替换密码,但每个字符都被字母表中“其右侧”的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(混乱):
字母表='abcdefghijklmnopqrstuvxyz'
加密=“”
对于混乱中的字符:
如果char='':
加密=加密+“”
其他:
旋转索引=字母表索引(字符)+13
如果指数<26:
加密=加密+字母表[旋转索引]
其他:
加密=加密+字母[旋转索引%26]
返回加密
def main():
打印(rot13(“abcde”))
打印(rot13(“nopqr”))
打印(rot13(rot13(“因为rot13是对称的,所以您应该看到此消息”))
如果名称=“\uuuuu main\uuuuuuuu”:
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()
def rot13(混乱,字符):
字母表='abcdefghijklmnopqrstuvxyz'
加密=“”
对于混乱中的字符:
如果char='':
加密=加密+“”
其他:
旋转索引=字母表索引(字符)+混乱
如果指数<26:
加密=加密+字母表[旋转索引]
其他:
加密=加密+字母[旋转索引%26]
返回加密
def main():
凌乱=输入(“旋转方式:”)
_消息=输入(“键入消息”)
打印(rot13(信息,乱七八糟)
如果名称=“\uuuuu main\uuuuuuuu”:
main()

我不知道我的输入应该在函数中的何处发生。我觉得可以加密吗

这可能就是你要找的。它通过
messy\u 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))
def rot13(混乱,旋转):
字母表='abcdefghijklmnopqrstuvxyz'
加密=“”
对于混乱中的字符:
如果char='':
加密=加密+“”
其他:
旋转索引=字母表。索引(字符)+整数(旋转)
如果指数<26:
加密=加密+字母表[旋转索引]
其他:
加密=加密+字母[旋转索引%26]
返回加密
def main():
凌乱=输入(“旋转方式:”)
_消息=输入(“键入消息”)
打印(rot13(信息,乱七八糟)
def rot(消息,旋转方式):
'''
创建一个字符串作为旋转给定字符串的结果
按要旋转的给定字符数
Args:
消息:要按字符旋转的字符串
旋转:表示数字的非负整数
要旋转消息的字符数
返回:
表示给定字符串(消息)的字符串,按
(按)字符旋转。例如:
rot('hello',13)返回'uryyb'
'''
断言isinstance(旋转,int)=真
断言(按>=0旋转)=True
字母表='abcdefghijklmnopqrstuvxyz'
旋转的_消息=[]
对于消息中的字符:
如果char='':
旋转的_消息。追加(字符)
其他:
旋转索引=字母表索引(字符)+旋转索引
如果指数<26:
旋转的消息。追加(字母表[旋转的索引])
其他:
旋转的\u消息。追加(字母表[旋转的\u索引%26])
返回“”。加入(旋转消息)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
尽管如此:
#获取执行rot函数所需的用户输入
消息=输入(“在此处输入消息:”)
rotate_by=输入(“输入您的轮换编号:”)
#确保用户的输入有效
如果没有,则按.isdigit()旋转\u:
打印(“无效!应为旋转的非负整数”)
持续
旋转消息=旋转(消息,整数(旋转)
打印(“旋转消息:”,旋转消息)
#允许用户决定是否继续
再次运行_=input(“继续?[y/n]:”)。下()
再次运行时!='然后再跑n':
打印(“无效!应为“y”或“n”)
再次运行_=输入(“继续?[y/n]:”)
如果再次运行_='n':
打破

注意:更有效的方法是创建一个字符列表,然后将它们连接起来生成一个字符串,而不是使用
string=string+char
。请参阅方法6和Python文档。另外,请注意,我们的rot函数只适用于字母表中的小写字母。如果您试图使用大写字符或任何不在
字母表中的字符来破坏消息,则消息将被破坏
用于混乱中的字符
。。。您正在重写char参数。。。它甚至不是一个字符,它是
messy_shit
,看起来你想用作一个数字,但输入总是返回一个字符串
rotated_index=alphabet。index(char)+mess
可能不正确。我知道这可能是家庭作业,但python已经提供了rot13。我试过了,成功了!除了一件事。当我删除了主函数并有了松散的li时,它就工作了
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