167k views
1 vote
1|>def average_evens(start, end):

2|>>sum = 0
3|>>count = 0
4|>>for i in range(start, end + 1): 5|>>>[fill in this blank]
6|>>return sum / count
Q: The function average_evens should return the average of all the even numbers between start and end (inclusive).
Which of the following segments would correctly complete this function?

User RDJ
by
8.8k points

1 Answer

1 vote

Final answer:

To complete the function average_evens, we need to check if each number in the range is even. If a number is even, we can add it to the sum and increase the count by 1. The correct segment that completes the function is if i % 2 == 0: sum += i count += 1. Finally, we return the average by dividing the sum by the count.

Step-by-step explanation:

To complete the function average_evens, we need to check if each number in the range is even. If a number is even, we can add it to the sum and increase the count by 1. Here is the correct segment that completes the function:

if i % 2 == 0:

sum += i

count += 1

This segment checks if i is divisible by 2, which means it is an even number. If it is, we add it to the sum and increase the count. Finally, we return the average by dividing the sum by the count.

User Alex Laskin
by
8.2k points

No related questions found