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

Indentation error报错?Python新手写甜点识别代码运行异常求解决

代码问题修复说明

现存问题

  • 缩进逻辑错误:原代码最后询问「是否蘸糖浆」的else分支,错误绑定到了if answer=='No'(判断是否为圆形炸面团的分支)上,导致第一个问题回答Yes的场景会异常触发后续无关询问,逻辑完全混乱
  • 分支覆盖不全:所有用户回答No的末端分支没有兜底提示,部分原料组合场景没有对应输出逻辑,就会出现无结果打印的情况
  • 输入兼容性差:仅支持严格匹配首字母大写的Yes/No,用户输入小写、带空格等格式都会直接跳出所有判断无输出

修复后可运行代码

print('Let me see what dessert you got!')
# 统一输入处理,去掉前后空格转小写,兼容用户不同输入格式
answer = input('Is your dessert round fried dough? ').strip().lower()
if answer == 'yes':
    answer2 = input('Is it dipped in syrup? ').strip().lower()
    if answer2 == 'yes':
        print('Your dessert is loukoumades')
    elif answer2 == 'no':
        answer3 = input('Is it dusted in sugar? ').strip().lower()
        if answer3 == 'yes':
            print('Your dessert is a donut')
        else:
            print('No matching dessert found for your description')
elif answer == 'no':
    answer4 = input('Is your dessert thin fried dough? ').strip().lower()
    if answer4 == 'yes':
        answer3 = input('Is it dusted in sugar? ').strip().lower()
        if answer3 == 'yes':
            print('Your dessert is churros')
        else:
            print('No matching dessert found for your description')
    elif answer4 == 'no':
        answer5 = input('Is it dipped in syrup? ').strip().lower()
        if answer5 == 'yes':
            print('Your dessert is a jalebi')
        else:
            print('No matching dessert found for your description')
else:
    print('Please enter only Yes/No as your answer')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 13:54:09