Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查列表时出错_Python - Fatal编程技术网

Python 检查列表时出错

Python 检查列表时出错,python,Python,我试图检查列表中是否存在9,但不断出现以下错误: “列表索引超出范围” 我的代码: “nums”应该只是一个列表。ΑlsoĜ我对python和一般编程都是新手。非常感谢您的帮助 如果您不知道“NUM”列表的长度,可以说: def array_front9(nums): for num in nums: if num==9: return True else: return False 编辑:或者更确切地说,正如@Patrick Haugh和@chrisz

我试图检查列表中是否存在9,但不断出现以下错误: “列表索引超出范围” 我的代码:


“nums”应该只是一个列表。ΑlsoĜ我对python和一般编程都是新手。非常感谢您的帮助

如果您不知道“NUM”列表的长度,可以说:

def array_front9(nums):
    for num in nums:
        if num==9: return True
        else: return False
编辑:或者更确切地说,正如@Patrick Haugh和@chrisz所建议的:

def array_front9(nums): return 9 in nums

nums的内容是什么?看起来它的项目少于3个。
nums
可能有<3个元素。真正的问题是为什么不在nums中使用
9
?@MikeScotty这似乎是个问题,让我来解决that@PatrickHaugh现在尝试这种方法错误“无法分配到文字”只是为了检查:添加
print(nums)
作为函数的第一行,看看
nums
是否真的是一个列表。
在nums中返回9
def array_front9(nums): return 9 in nums