Function which returns Sum of all arguments

Wazowski

I want bo create a function which returns sum of all passed arguments.

I found similar problem on forum, but It doesn't work with last case.

def sum_all(*args):
    sum = 0

    for num in args:
        if not str(num).isdigit():
            return False
        else:
            sum += int(num)
    return sum

This code work with first and second case, but return false on third case.

sum_all(2,-3)
# -1
sum_all([3,4,5])
# 12
sum_all(1,2,[3,4,5])
# false 

How can i make it work? To make it return 15 in last case? Thanks!

Simona Chovancová

In case of a list, just call the function recursively, example:

def sum_all(*args):
  sum = 0
  for num in args:
    if type(num) is list:
      sum += sum_all(*num)
    else:
      sum += int(num)
  return sum

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to define a function which returns sum of functions?

Wrapper function which returns promise irrespective of function arguments

How to pass a variable which has multiple arguments to function all at once?

Write a function that returns a Sum of all palindrome numbers present in an Array

Recursive function that returns sum of all odd numbers within a range

In which model to put a function that returns all active / inactive venues of the user?

haskell Function taking 2 arguments returns function which takes one argument?

Sum function returns zero

Lisp Function that Returns a Sum

Scala: Is there a way to write a generic function which returns a generic instance with generic arguments?

Does R have a base function that takes a predicate and a vector as arguments, and returns the first member for which the predicate is true?

Is it possible to write a function template which returns whether the number of arguments is divisible by N?

function pointer prototype which takes three integer pointers as arguments and returns character pointer

Function which returns function scheme

Deepcopy all function arguments

Function Which Returns Sum of Diagonal Elements of 2D Array in C++

A method or function that returns a list of pairs of numbers which would sum up to 5

Change checkUpkeep function returns arguments

Function which takes an integer k that checks if a list of integer lists l, returns the count of the number of integer lists in l, which sum to k

Testing a function which returns an object that returns a function which return a boolean

MYSQL results which contains strings but returns a sum

Implement a function which returns a pointer

A Function Which Returns Parent Functions

Function which returns multiple values

Write a recursive function which will calculate the sum of all numbers from 1 to n - swift

Python function that takes a positive integer n and returns the sum of the squares of all the positive integers smaller than n

Find the sum of data stored in all non leaf of Binary Search Tree? (a standalone recursive function that returns an integer.)

Having trouble filling in empty function so it returns the sum of all divisors, without including it

SQL Server : user-defined function that returns the sum of all the values corresponding to an employeeID