简体   繁体   English

在 python 中实现 rot13

[英]Implementing rot13 in python

I am trying to implement the rot13 algorithm.我正在尝试实现 rot13 算法。 Obviously not hard but my issue is with strings that have an apostrophe in python I don't know what to do to fix this.显然不难,但我的问题是在 python 中带有撇号的字符串我不知道如何解决这个问题。 I am not sure if the issue is with my program or with the way python runs because I tried running an empty program like so "python3 solve.py ' " and it didn't run but gives me a > in the terminal.我不确定问题是出在我的程序上还是出在 python 的运行方式上,因为我尝试运行一个像“python3 solve.py '”这样的空程序,但它没有运行,但在终端中给了我一个 >。 I'll also add my code below if anyone sees something else wrong.如果有人看到其他错误,我也会在下面添加我的代码。 By empty program, I just call a main that does nothing.通过空程序,我只是调用一个什么都不做的主程序。

import sys


def main(input):
    output = ""
    abc = "abcdefghijklmnopqrstuvwxyz"
    ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    for i in input:
        if ord(i) in range(65,91):
            output += ABC[((ABC.find(i) + 13) % 26)]
        elif ord(i) in range(97,123):
            output += abc[((abc.find(i) + 13) % 26)]
        else:
            output += i
    return output

if __name__ == '__main__':

    result = main(sys.argv[1])

    print(result)

Single apostrophe serves as a start of quoted, possibly multiline, string and that is the reason for the > prompt you see.单撇号用作引用字符串的开头,可能是多行字符串,这就是您看到>提示的原因。 Too send a single apostrophe you can either escape it or quote it with double quotes:也可以发送一个撇号,您可以转义它或用双引号将其引用:

$ python solve.py \'
'
$ python solve.py "'"
'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM