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

Python输入无效时如何返回首行重输?输入yes却提示不理解怎么修复

错误原因
  • 核心问题出在第5行的条件判断:你调用字符串转小写的lower方法时遗漏了括号,写为了user_reply.lower == 'yes'。lower是字符串的内置方法,必须加()才会执行转换操作返回小写字符串,不加括号时你比对的是「方法对象本身」和字符串yes,永远返回False,因此输入yes也会走到else分支打印I do not understand。
  • 额外可优化点:当前代码执行完for_calculation()后没有退出循环,会重新弹出初始问题询问,如果你需要执行完计算就结束程序,可以在调用for_calculation()后加break或者exit()调整逻辑。
修复后的代码
print('Hi, I am your bot, James!')

# 示例实现你自定义的for_calculation函数,如果你已经定义过可以删掉这段
def for_calculation():
    print("执行计算逻辑")
    # 按需在这里补充你的计算代码

while True:
    user_reply=input("Are you ready for today's activity? ")
    # 修复:lower方法后补充调用括号
    if user_reply.lower() == 'yes':
        for_calculation()
        # 如果执行完要退出程序就保留下一行,不需要可以删除
        exit()

    elif user_reply.lower() == 'no':
        while True:
            double_confirm=input('Are you sure? ')
            if double_confirm.lower() == 'yes':
                print('See you next time.')
                exit()
            elif double_confirm.lower() == 'no':
                for_calculation()
                # 同上,需要退出就保留下一行
                exit()
            else:
                print('I do not understand.')

    else:
        print('I do not understand.')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 17:24:05