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

a) True
b)False

1 Answer

4 votes

Final answer:

To write a sum_odds function that returns the sum of odd values in a list, create a variable called sum and set it to 0. Iterate through each number in the given list and check if it is odd using the modulo operator. If the number is odd, add it to the sum variable. Return the sum.

Step-by-step explanation:

To write a sum_odds function in Python, you can use the following steps:

Create a variable called sum and set it to 0.Iterate through each number in the given list.Check if the number is odd using the modulo operator (%). If the number is odd, add it to the sum variable.Return the value of the sum.

Here's an example of how to implement this function:

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

numbers = [1, 2, 3, 4, 5]
print(sum_odds(numbers))

The function sum_odds takes a list of numbers as an argument and returns the sum of the odd values in the list. In the example above, the function will return 9 since the odd values in the list are 1, 3, and 5.

User Lady Aleena
by
7.3k points