python中的另一个caesar密码

python中的另一个caesar密码,python,Python,我正在写一个函数,将文本移位13个空格。转换后的字符需要保留大小写,如果字符不是字母,那么它们应该不移位地通过。我编写了以下函数: def rot13(str): result = "" for c in str: if 65 <= ord(c) <= 96: result += chr((ord(c) - ord('A') + 13)%26 + ord('A')) if 97 <= ord(c) <

我正在写一个函数,将文本移位13个空格。转换后的字符需要保留大小写,如果字符不是字母,那么它们应该不移位地通过。我编写了以下函数:

def rot13(str):
    result = ""
    for c in str:
        if 65 <= ord(c) <= 96:
            result += chr((ord(c) - ord('A') + 13)%26 + ord('A'))
        if 97 <= ord(c) <= 122:
            result += chr((ord(c) - ord('a') + 13)%26 + ord('a'))
        else:
            result += c
    print result
def rot13(str):
result=“”
对于str中的c:
如果65您缺少“else”语句,那么如果第一个if“fires”(
c
是大写字母),那么第二个if的“else”也会“fires”(并连接大写字母,因为
ord(c)
不在
97
122
之间)


通过这种方式,它只假设小写和大写字母排列在一致的块中,而与它们的实际值无关。

这肯定是问题所在。谢谢
def rot13(str):
    result = ""
    for c in str:
        if 65 <= ord(c) <= 96:
            result += chr((ord(c) - ord('A') + 13)%26 + ord('A'))
        elif 97 <= ord(c) <= 122:
            result += chr((ord(c) - ord('a') + 13)%26 + ord('a'))
        else:
            result += c
    print result
def rot13(str):
    a = ord('a')
    z = ord('z')
    A = ord('A')
    Z = ord('Z')
    result = ""
    for c in str:
        symbol = ord(c) 
        if A <= symbol <= Z:
            result += chr((symbol - A + 13)%26 + A)
        elif a <= symbol <= z:
            result += chr((symbol - a + 13)%26 + a)
        else:
            result += symbol
    return result