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

Python判断列表含数字9函数始终返回False问题排查

代码错误原因
  • 你判断的是字符串"9"是否在列表中,而列表里存储的是整数类型的9,Python中不同类型的值做相等判断时结果为假,因此"9" in nums的结果永远是False,导致函数返回值始终不符合预期。
修正后的代码
def find_nine(nums):
    if 9 in nums:
        return True
    else:
        return False
    

nums=[1,9,4,5,6]
print(find_nine(nums))

也可以直接简化判断逻辑,代码更简洁:

def find_nine(nums):
    return 9 in nums
验证结果
  • 输入示例1 [1,2,9,4,5],输出为True,符合预期
  • 输入示例2 [1,2,4,5],输出为False,符合预期

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.19 16:54:03