Ask Question
15 November, 22:43

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

+4
Answers (1)
  1. 15 November, 22:58
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers