Question
Write a Python function to add all the even numbers in a list that is passed as a parameter. Follow the steps given below. 1).
Write a Python function to add all the even numbers in a list that is passed as a parameter. Follow the steps given below. 1). Define the header for a function named add(). The function should have a single parameter named numbers. Suppose we have a while loop to accumulate the sum of a sequence of numbers. The sum is to be stored in a variable named total. Then, which of the following is true?
a) The variable total needs to be used inside the while loop
b) The variable total needs to be initialized before entering the while loop
c) Both (a) and (b)
d) Neither (a) nor (b)
2). The correct answer is (c). To solve our problem, however, we can use a for loop.
3). Consider the following two (adjusted) sentences in the question and its answer. a) The sum is to be stored in a variable named total.
b) The variable total needs to be initialized before entering the for loop.
4). The for loop should iterate over all the elements of the list. How do you do this? Write the code below. 5). The code must select the even numbers.
a) How would you check if a number is even?
b) Which number should you check?
Write Python code for that. 6) we see that the variable total needs to be used inside the for loop. How would you do that? 7) Return the total value. Where should the code be? Code it. 8) Put together all the code to get your function.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started