0

I would like to create a function that take a list and sum all number in the list. For instance, in the list ["a", 1.50, 2, u'week', 250, 12], the function will return the output 255.50 (1.50 + 2 +250 +12).

def sum(list):
   sum = 0
   for item in list:
      if type(item) == 'a number'
         sum += item
   return sum

By what could I replace the line if type(item) == 'a number'?

1 Answers1

1

Try the following

from numbers import Number

if isinstance(item, Number):
Robert Jacobs
  • 3,266
  • 1
  • 20
  • 30