How to write a python function that adds all arguments?

前端 未结 3 2013
日久生厌
日久生厌 2021-01-03 00:23

I\'d like to write a python function which adds all its arguments, using + operator. Number of arguments are not specified:

def my_func(*args):
         


        
3条回答
  •  借酒劲吻你
    2021-01-03 01:13

    How about this:

    def my_func(*args):
        my_sum = 0
        for i in args:
            my_sum += i
        return my_sum
    

    If you don't want to use the += operator, then

    my_sum = my_sum + i
    

提交回复
热议问题