求大神帮忙看哪错了
def my_func(L):
B=0
C=1
for A in L:
if isinstance(A,int) or isinstance(A,float):
if isinstance(L,list):
B=A+B
return B
if isinstance(L,tuple):
C=A*C
return C
L=[1,2,3,4]
print(my_func(L))
def my_func(L):
B=0
C=1
for A in L:
if isinstance(A,int) or isinstance(A,float):
if isinstance(L,list):
B=A+B
return B
if isinstance(L,tuple):
C=A*C
return C
L=[1,2,3,4]
print(my_func(L))
2024-01-12
你应该在 for 循环完之后再输出 B 和 C,而不是在第一次 if 判定之后就 return。所以最好这样:
def my_func(L): B = 0 C = 1 is_list = isinstance(L, list) is_tuple = isinstance(L, tuple) for A in L: if isinstance(A, int) or isinstance (A, float): if is_list: B += A elif is_tuple: C *= A else: return None if is_list: return B if is_tuple: return C L = [1, 2, 3, 4] print(my_func(L))
举报