104k views
5 votes
Write a function called sum_odds that accepts a list of integers as an argument and returns the sum of all odd values in the list.

User Jgibson
by
8.0k points

1 Answer

4 votes

Final answer:

To find the sum of odd values in a list, you can iterate through the list and add the odd elements to a sum variable.

Step-by-step explanation:

To solve this problem, you can iterate through the list and check each element if it is odd. If the element is odd, you can add it to a sum variable. Finally, return the sum after iterating through the entire list. Here's an example function:

def sum_odds(lst):
sum = 0
for num in lst:
if num % 2 != 0:
sum += num
return sum

# Example usage
my_list = [1, 2, 3, 4, 5]
result = sum_odds(my_list)
print(result) # Output: 9

User Joland
by
7.7k points