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.

Respuesta :

Answer:

def sum_numbers(value_list):

   total = 0

   for n in value_list:

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

           total += n

   return total

Explanation:

Create a function called sum_numbers that takes one parameter, value_list

Initialize the total as 0

Create a for loop that iterates through the value_list

Inside the loop, check the type of the elements. If they are either int or float, add them to total.

When the loop is done, return the total

The Python program code to calculate the addition of the list values by using the method that is "Sum" can be defined as follows:

Program:

def Sum(value_list):#defining a method Sum that takes value_list as a list in parameter

  s = 0#defining s variable that initilizes with 0

  for n in value_list:#defining a for loop that adds list values

      if type(n) == int or type(n) == float:#defining if block that check list value and adds list value in s variable

          s += n#addining list value and holds its value in s variable

  return s#return s variable value

value_list=[20,-10,30,-9,55.90,70.50,-60,0]#defining a list value_list that holds value

print(Sum(value_list))#calling method and prints its sum value

Output:

please find the attached file.

Program Explanation:

  • Defining the method "Sum" that takes a list "value_list" inside the parameters.
  • Inside the method, an integer variable "s", and a for loop is declared.
  • Inside the loop, it uses the list value and adds its value to the "s" variable, and returns its value.
  • Outside the method, a list is defined that holds a value and passes into the method, and prints its return value.

Find out more about the list here:

brainly.com/question/17019263

Ver imagen codiepienagoya