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

右移5位Basic Caesar Cipher的Python多轮输入实现问题求助

凯撒密码(右移5位)多轮输入实现方案

完整实现代码

alphabet = "abcdefghijklmnopqrstuvwxyz"
key = "fghijklmnopqrstuvwxyzabcde"
encrypted_msg = []

while True:
    current_input = input("Enter a letter: ")
    # 空输入(直接回车)时终止循环
    if not current_input:
        break
    # 处理输入字符
    for char in current_input:
        lower_char = char.lower()
        if lower_char in alphabet:
            encrypted_char = key[alphabet.find(lower_char)]
            # 保留原字符大小写
            encrypted_msg.append(encrypted_char.upper() if char.isupper() else encrypted_char)
        else:
            encrypted_msg.append(char)

# 按示例要求用空格分隔输出加密结果
print(" ".join(encrypted_msg))

功能说明

  • 新增while True无限循环,支持持续接收用户输入,遇到直接回车的空输入自动终止循环
  • 若需要将输入全空格的情况也判定为终止条件,可将空输入判断条件修改为if not current_input.strip()
  • 用列表存储每一次加密后的字符,相比字符串拼接性能更优,也方便后续调整输出格式
  • 保留了原代码的非字母字符原样输出逻辑,新增大小写适配,输入大写字母输出也为对应大写
  • 最终输出时用空格分隔所有加密字符,和你给出的示例运行效果完全匹配:依次输入a、b、c后回车,输出结果为f g h

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 19:45:07