这是凯撒密码,为什么打印偏移后的字符里空格处多了一个字符

img


这是凯撒密码,为什么打印偏移后的字符里空格处多了一个字符,为什么啊,我是初学者,希望有简单易懂的回答,感谢🙏

当执行遇到空格时,会执行else后面的代码,再加上1个空格,可以放在前面,加个continue继续下个循环

def encypt_func(txt, s):
    result=""
    for i in range(len(txt)):
        if (txt[i] == " "):
            result += " "
            continue
        if (txt[i].isupper()):
            result += chr((ord(txt[i]) + s - 65) % 26 + 65)
        else:
            result += chr((ord(txt[i]) + s - 97) % 26 + 65)  
    return result