def sub_sum(num): sum1=0 sum2=0 if num%2==0: sum1=num+sum1 else: sum2=num+sum2 return sum1,sum2 L=[1,2] for i in L: result=sub_sum(i) print(result)
你函数里面不写for循环的嘛,判断是否为偶数的前面加个list的for循环就好了
sum2 = 0
因为每次调用sub_sum(num)函数时,sum1和sum2都会重新被定义一次