Python中的Caesar密码编码器

Python中的Caesar密码编码器,python,string,python-3.x,encryption,Python,String,Python 3.x,Encryption,我正试图用python编写一个简单的程序,使用Caesar密码对字符串进行特定移位编码,但我的代码似乎无法正常工作 这是我的密码: n = int(input()) s = list(input()) for i in range(len(s)): if s[i] == " ": new = 32 else: if s[i].isupper(): new = ord(s[i]) + n while n

我正试图用python编写一个简单的程序,使用Caesar密码对字符串进行特定移位编码,但我的代码似乎无法正常工作

这是我的密码:

n = int(input())
s = list(input())
for i in range(len(s)):
    if s[i] == " ":
        new = 32
    else:
        if s[i].isupper():
            new = ord(s[i]) + n
            while new > 122:
                new -= 26
        else:
            new = ord(s[i]) + n
            while new > 90:
                new -= 24
    s[i] = chr(new)
print ("".join(s))
你认为这里出了什么问题

测试用例是:

十二,

Qx Bek Oazsdaa

这应返回:


你的122和90在错误的位置,24应该是26

n = int(input())
s = list(input())
for i in range(len(s)):
    if s[i] == " ":
        new = 32
    else:
        if s[i].isupper():
            new = ord(s[i]) + n
            while new > 90:
                new -= 26
        else:
            new = ord(s[i]) + n
            while new > 122:
                new -= 26
    s[i] = chr(new)
print ("".join(s))
结果:

2
Hello abc XYZ
Jgnnq cde ZAB

仍然失败的案例:(这些案例是什么?