Ask Question
7 January, 09:08

If you have a list consisting of just numbers, then you can add all of the values in the list using the sum () function.

If your list consists of some numbers and some values of other types (e. g., lists, strings, sets), the sum () function will fail. In this question, we're asking you to write a function that will sum all of the numbers in a list, ignoring the non-numbers. The function below takes one parameter: a list of values (value_list) of various types.

The recommended approach for this:

(1) create a variable to hold the current sum and initialize it to zero,

(2) use a for loop to process each element of the list,

(3) test each element to see if it is an integer or a float, and, if so, add its value to the current sum,

(4) return the sum at the end.

student. py 1

Hef sum_lengths (value_list):

# Implement your function here. Be sure to indent your code block! Restore original file

+5
Answers (1)
  1. 7 January, 11:17
    0
    See explaination

    Explanation:

    def sum_lengths (value_list):

    total = 0

    for x in value_list:

    if (type (x) = =int) or (type (x) = =float):

    total + = x

    return total
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “If you have a list consisting of just numbers, then you can add all of the values in the list using the sum () function. If your list ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers