Given below is a Python program that takes a list of numbers as input from the user and prints the sum of all the even numbers in the list. You can assume that the user enters only valid numbers and the list is not empty. Fill in the blanks in the following code to complete the program.
# A function that returns True if a number is even, False otherwise
def
is_even
(num):
_________________
# Missing statement 1
# A function that returns the sum of all the even numbers in a list
def
sum_even
(lst):
# Initialize a variable to store the sum
total =
0
# Loop through each element in the list
for
num
in
lst:
# Check if the element is even using the is_even function
if
_____________
# Missing statement 2
# Add the element to the total
______________
# Missing statement 3
# Return the total sum
_________________
# Missing statement 4
# Take a list of numbers as input from the user
lst = list(map(int, input(
"Enter a list of numbers: "
).split()))
# Print the sum of all the even numbers in the list using the sum_even function
print("The sum of all the even numbers in the list is", sum_even(lst))
Answer:
Answer by student
# A function that returns True if a number is even, False otherwise
def
is_even
(num):
return
num %
2
==
0
# Missing statement 1
# A function that returns the sum of all the even numbers in a list
def
sum_even
(lst):
# Initialize a variable to store the sum
total =
0
# Loop through each element in the list
for
num
in
lst:
# Check if the element is even using the is_even function
if
is_even(num)
# Missing statement 2
# Add the element to the total
total += num
# Missing statement 3
# Return the total sum
return
total
# Missing statement 4
# Take a list of numbers as input from the user
lst = list(map(int, input(
"Enter a list of numbers: "
).split()))
# Print the sum of all the even numbers in the list using the sum_even function
print(
"The sum of all the even numbers in the list is"
, sum_even(lst))
Detailed answer by teachoo
- The question requires us to write two functions: one that checks if a number is even, and another that sums up all the even numbers in a list.
- The first function, is_even , takes a parameter num which is a number. It returns True if num is divisible by 2, and False otherwise. To check if a number is divisible by 2, we can use the modulo operator ( % ) which gives the remainder of the division. If the remainder is 0, then the number is divisible by 2. So, we can write:
def is_even(num):
return num % 2 == 0
- This is the missing statement 1 in the code.
- The second function, sum_even , takes a parameter lst which is a list of numbers. It returns the sum of all the even numbers in the list. To do this, we need to do the following steps:
- Initialize a variable total to store the sum. We can start with 0 as the initial value.
- Loop through each element in the list using a for loop. We can use the variable num to refer to each element.
- Check if the element is even using the is_even function. If it is, then add it to the total variable using the assignment operator ( += ) which adds and assigns the value in one step.
- Return the total variable after the loop ends.
- So, we can write:
def sum_even(lst):
# Initialize a variable to store the sum
total = 0
# Loop through each element in the list
for num in lst:
# Check if the element is even using the is_even function
if
is_even(num):
# Add the element to the total
total += num
# Return the total sum
return total
- These are the missing statements 2, 3 and 4 in the code.
- The last part of the code takes a list of numbers as input from the user and prints the sum of all the even numbers in it using the sum_even function. To do this, we need to do the following steps:
- Use the input function to get a string from the user. We can pass a prompt message as an argument to this function.
- Use the split method to split the string into a list of substrings based on whitespace characters (such as space, tab, newline, etc.). For example, "1 2 3".split() will give ["1", "2", "3"] .
- Use the map function to apply another function (such as int ) to each element in a list and return a new list with those values. For example, map(int, ["1", "2", "3"]) will give [1, 2, 3] .
- Use the list function to convert any iterable (such as a map object) into a list. For example, list(map(int, ["1", "2", "3"])) will give [1, 2, 3] .
- Assign this list to a variable named lst .
- Use the print function to display a message and the result of calling sum_even on lst . We can use commas to separate multiple arguments to this function.
So, the final code is:
# A function that returns True if a number is even, False otherwise
def
is_even
(num):
return
num %
2
==
0
# A function that returns the sum of all the even numbers in a list
def
sum_even
(lst):
# Initialize a variable to store the sum
total =
0
# Loop through each element in the list
for
num
in
lst:
# Check if the element is even using the is_even function
if
is_even(num):
# Add the element to the total
total += num
# Return the total sum
return
total
# Take a list of numbers as input from the user
lst = list(map(int, input(
"Enter a list of numbers: "
).split()))
# Print the sum of all the even numbers in the list using the sum_even function
print("The sum of all the even numbers in the list is", sum_even(lst))