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

Python问答程序正确输入未匹配响应的问题修复

Python问答测验程序修复方案

问题原因

  1. 大小写不匹配:你将用户输入转为小写variable2.lower(),但对比的list[0]是大写开头的"Mariah",两者无法匹配,导致正确输入也触发错误提示。
  2. 缩进语法错误:第二个if分支下的print语句没有缩进,不符合Python的代码块规范,会导致程序报错。

修复后的代码

friend_list = ["Mariah", "Julian", "Gabbi"]
response = input("do you know my friends? yes or no? ")
if response.lower() == "yes":
    print("oh really... ")
    best_friend = input("what is my best friend's name? ")
    # 统一转为小写进行匹配,兼容不同大小写输入
    if best_friend.lower() == friend_list[0].lower():
        print("you are correct, but she's not my only friend")
    else:
        print("sorry you're WRONG!")
elif response.lower() == "no":
     print("i thought so...")
else:
    print("that answer didn't make sense")

修复说明

  • 将列表名从list改为friend_list,避免和Python内置类型重名
  • 把用户输入和列表元素统一转为小写对比,支持任意大小写的正确输入(如"mariah"、"MARIAH"都能匹配成功)
  • 修复了第二个if分支下print语句的缩进问题,确保代码块逻辑正确

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 15:07:44