问答详情
源自:9-5 Python函数参数

为什么我只会输出'error'和None

def func(l):

    if not isinstance(l,list) or not isinstance(l,tuple):

        print ('error')

        return None

    elif isinstance(l,list):

        sum = 0

        for item in l:

            if isinstance(item,int) or isinstance(item,float):

                sum += item

        return sum

    elif isinstance(l,tuple):

        sum = 1

        for item in l:

            if isinstance(item,int) or isinstance(item,float):

                sum = sum * item

        return sum

    return None


提问者:qq_hush_ceklpC 2021-07-15 23:20

个回答

  • 慕村4040375
    2021-07-21 15:44:54
    已采纳

    not isinstance(L,list) or not isinstance(L,tuple)

    这个条件相当于L既是list也是tuple才是false,实际上L只能是list,或者只能是tuple,所以条件就永远都是true

    可以试试改成not isinstance(L,list) and not isinstance(L,tuple),或者not (isinstance(L,list) or isinstance(L,tuple))