i need to solve a task:
a function filter_list that takes only one argument (a list of integers) and returns two values in a tuple
- sum of elements of the list that are >= 13 and <= 97
- sum of elements of the list that are >0 and 13-fold
So i have a conceptual understanding of how that could be achieved, however when i start writing my code, i can't assign those two values to a new variable outside the function.
numbers = [1, 10, 13, 23, 34, 45, 90]
def filter_list(nums):
for e in nums:
result1 = sum(e for e in nums if e >= 13 and e <= 97)
return result1
for e in nums:
result2 = sum(e for e in nums if e>0 and e % 13 == 0)
return result2
print(result1, result2)
print(filter_list(numbers))
result_sum = filter_list(numbers)
print(result_sum)
returnstatement the function exits and won't continue running. So for the first iteration of your first for loop, it will exit. It will never reach any other iteration and it certainly won't hit the second for loop.