简体   繁体   中英

Implementing rot13 in python

I am trying to implement the rot13 algorithm. 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. 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. 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 "'"
'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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