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

为何底部的while循环仅执行一次?凯撒密码程序问题排查

凯撒密码程序循环逻辑问题修复

原代码

alphabet = ['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', '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']

def caesar(text, shift, direction):
  cipher_text = ""
  for letter in text:
    text_position = alphabet.index(letter)
    if direction == "encode":
      new_text_position = text_position + shift
    elif direction == "decode":
     new_text_position = text_position - shift
    else:
      print("Error, invalid entry.")
    cipher_text += alphabet[new_text_position]
  print(f"Your cipher text is {cipher_text}")

direction = input("Type 'encode' to encrypt, or 'decode' to decrypt:\n")
while direction != "encode" or direction != "decode":
  direction = input("Invalid entry. Type 'encode' to encrypt, or 'decode' to decrypt:\n")
  if direction == "encode" or direction == "decode":
    text = input("Type your message:\n").lower()
    shift = int(input("Type the shift number:\n"))
    caesar(text, shift, direction)

问题描述

这是Udemy平台Angela Yu的Python课程中的凯撒密码程序,当前存在两个核心问题:

  • 即便输入合法的encode或decode,程序仍会强制进入while循环执行一次,无法直接进入后续处理流程
  • 原循环结构将业务处理逻辑嵌套在循环内部,导致首次合法输入时会跳过处理步骤

错误原因分析

  1. 循环条件逻辑错误:原条件direction != "encode" or direction != "decode"永远为真——任何字符串不可能同时等于encode和decode,因此无论输入是否合法,循环都会触发。正确逻辑应为:仅当输入既不是encode也不是decode时才进入循环,需用and连接判断条件。
  2. 处理逻辑位置错误:原代码将文本输入、偏移量输入及加密解密调用放在循环内部,导致只有在循环中修正输入为合法值时才会执行处理,首次直接输入合法值时会跳过核心业务流程。

修正后的代码

alphabet = ['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', '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']

def caesar(text, shift, direction):
    cipher_text = ""
    for letter in text:
        text_position = alphabet.index(letter)
        if direction == "encode":
            new_text_position = text_position + shift
        elif direction == "decode":
            new_text_position = text_position - shift
        else:
            print("Error, invalid entry.")
            return  # 非法方向时直接终止函数,避免后续字符拼接报错
        cipher_text += alphabet[new_text_position]
    print(f"Your cipher text is {cipher_text}")

# 先获取合法的操作方向输入
direction = input("Type 'encode' to encrypt, or 'decode' to decrypt:\n")
while direction != "encode" and direction != "decode":
    direction = input("Invalid entry. Type 'encode' to encrypt, or 'decode' to decrypt:\n")

# 输入合法后执行核心业务流程
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
caesar(text, shift, direction)

修改说明

  • 修正while循环条件为direction != "encode" and direction != "decode",确保仅在输入非法值时才触发循环重输
  • 将文本、偏移量输入及加密解密函数调用移至循环外部,保证所有合法输入都会触发核心处理流程
  • 给caesar函数添加return语句,当传入非法方向时直接终止函数,避免后续字符拼接操作报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 02:56:52