Skip to main content
 首页 » 编程设计

python之为什么我得到 'ValueError: too many values to unpack'

2025年05月04日24wayfarer

所以我有一些 python 代码 -

list1 = ["Anthony", "Sean"] # define a table to use while testing 
 
def IsInArray(val, *tab):   # define function to check 
    for v, in tab: 
        if v == val: 
            return true 
    return false 
 
if IsInArray("Anthony", list1) == true:        # testing it 
    print("Anthony is in list1") 
 
if IsInArray("Charles", list1) == true: 
    print("Charles is in list1") 

但它显示 ValueError: too many values to unpack。我不明白,其他线程差异太大,无法理解如何解决此问题。

请您参考如下方法:

只需使用 in 运算符:

if "Anthony" in list1:        # testing it 
    print("Anthony is in list1") 
 
if "Charles" in list1: 
    print("Charles is in list1")