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

Python自定义encrypt函数未被调用直接跳过,循环不打印内容是什么原因

问题原因

你的代码没有执行encrypt函数的核心原因是仅完成了函数定义,没有主动触发函数调用,和参数传递逻辑没有关系。

修复说明

  • Python中def关键字仅用于声明函数规则,只要没有写对应的函数调用语句,函数内部的代码永远不会被执行
  • 你当前的函数参数定义逻辑是正确的,只需要在函数定义的代码块结束后,补充一行调用语句即可正常运行
encrypt(direction, text, shift)

修复后的完整代码

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']

direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))


def encrypt(direction, text, shift):
    if direction == "encode":
        for index, letter in enumerate(text):
            print(letter)

# 新增函数调用语句
encrypt(direction, text, shift)

补充调用后,当你输入direction值为encode时,函数内的循环就会逐行打印你输入的文本的每一个字符。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 09:15:03