You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

凯撒密码函数修复及模运算序列循环机制技术咨询

解决凯撒密码输出问题并解释模运算作用

问题分析

你的代码目前有两个关键问题导致输出不符合预期:

  • 没有处理输入中的空格(非字母字符被直接忽略,没有加入结果)
  • 输出时用空格分隔每个加密后的字母,导致原本连续的字母被拆分,同时丢失了原输入的空格结构

修改后的代码

realText = input('please enter a string: ')
step = int(input('please enter step for shifting'))
def caesar_encrypt(realText, step):
    outText = []
    cryptText = []
    uppercase = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
    lowercase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    for eachLetter in realText:
        if eachLetter in uppercase:
            index = uppercase.index(eachLetter)
            crypting = (index + step) % len(uppercase)
            cryptText.append(crypting)
            newLetter = uppercase[crypting]
            outText.append(newLetter)
        elif eachLetter in lowercase:
            index = lowercase.index(eachLetter)
            crypting = (index + step) % len(lowercase)
            cryptText.append(crypting)
            newLetter = lowercase[crypting]
            outText.append(newLetter)
        else:
            # 处理非字母字符(比如空格),直接加入结果保留原格式
            outText.append(eachLetter)
    # 用空字符串拼接,避免额外添加空格,保留原输入的空格结构
    print(''.join(outText))
caesar_encrypt(realText, step)

修改点说明

  • 添加else分支:当遇到空格或其他非字母字符时,直接将其添加到输出列表outText中,不进行加密处理,完美保留原输入的空格布局。
  • 替换拼接方式:把(' ').join(outText)改为''.join(outText),取消了不必要的字母间空格,让加密后的字母保持连续,原输入的空格也会原样保留在结果里。

模运算符%实现循环的原理

模运算的核心是计算两个数相除后的余数,它能实现序列内循环移位的原因很直观:

  1. 大小写字母表的长度都是26,我们需要保证移位后的索引始终落在0-25的范围内(对应A-Z或a-z的位置)。
  2. 当index + step的和大于等于26时,(index + step) % 26会得到这个和除以26的余数,这个余数必然在0-25之间,相当于“绕回”字母表的开头继续计数。

举两个实际例子:

  • 假设当前字母是Z(大写索引25),偏移量step=13,计算25+13=38,38%26=12,对应字母M,实现了从字母表末尾回到中间的循环。
  • 再比如字母Y(索引24),偏移量step=3,24+3=27,27%26=1,对应字母B,完美完成了跨字母表末尾的循环移位。

内容的提问来源于stack exchange,提问作者MINO

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:24:08