def func(param): if isinstance(param, list): result = 0 for item in param: if isinstance(item, int) or isinstance(item, float): result += item return result elif isinstance(param, tuple): result = 1 for item in param: if isinstance(item, int) or isinstance(item, float): result *= item return result return None
参考思路
# coding=utf-8def func(param): #先判断是不是列表 if isinstance(param,list): result = 0 #如果是列表再把列表的每个值循环出来 for item in param: #再判断下循环出来的值是不是为整数和浮点数 if isinstance(item,int) or isinstance(item,float): result = result + item return result elif isinstance(param,tuple): result = 1 for item in param: if isinstance(item,int) or isinstance(item,float): result = result * item return result return None a = [1,2.5,3.5,6,8,4,1]b = (1,2.3,5)print(func(b))
如果是入参是 list,循环取值 list里面是int或者float 那么久所有值相加返回结果
如果是入参是 tuple,循环取值 tuple里面是int或者float 那么久所有值相乘返回结果