# -*- coding:utf-8 -*-
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
记录循环的次数,只要次数小于1,那么就return None
def func(param):
x = 0
if isinstance(param,list):
sum = 0
for item in param:
if isinstance(item,int) or isinstance(item,float):
sum += item
x += 1
if x < 1:
print('列表list内包含的数字元素个数为:{}'.format(x))
return None
else:
return sum
if isinstance(param,tuple):
ride = 1
for item in param:
if isinstance(item,int) or isinstance(item,float):
ride = ride * item
x += 1
if x < 1:
print('数组tuple内包含的数字元素个数为:{}'.format(x))
return None
else:
return ride
print('参数类型错误,请传参列表或数组')
return None
如果元组里面无数字类型,得到的结果就会为1,如果元组里面只有数字1,得到的结果还是1,想把这两个区分出来,你是怎么解决的
元组元素为0得情况也进了elif条件里 所以不包括0