根据函数中的参数数量返回不同的值

5 python python-3.x

我试图制作一个函数,它接受 1 到 5 个参数,并根据给定的数字进行不同的计算。我的想法是这样的:

def function(*args)
    num_of_args = (!!here is the problem!!)
if(num_of_args == 1) : result = a
else if(number_of_args == 2) : result = a+b
Run Code Online (Sandbox Code Playgroud)

依此类推,我试图计算参数的数量并将该数字分配给一个变量,但找不到我想象可能不需要使用 5 个 if 的方法,但我真的不想在我之前关注它设法计算这些参数

kdh*_*pak 5

您可以使用len(args).

def function(*args):
    if len(args) == 0:
        print("Number of args = 0")
    elif len(args) == 1:
        print("Number of args = 1")
    else:
        print("Number of args >= 2")
Run Code Online (Sandbox Code Playgroud)